meta.Validation class
Package: meta
Superclasses: handle
Describes property validation
Description
Instances of this class contain information about property validation that is
specified in a class definition. The meta.Validation
class enables
you to obtain the following information programmatically for each property in a class
definition:
Class restriction applied to the property
Size requirements of the property value
Function handles referencing validation functions applied to property values
For information on property validation, see Validate Property Values.
Properties
Methods
isValidValue
tf = isValidValue(metaValidationObj,value)
Determine if value is valid. This method returns true
if
value
is a valid value for the property whose validation is
describe by metaValidationObj
.
Input Arguments
metaValidationObj
- Themeta.Validation
object for the propertyvalue
- The potential property value to test for validity
Return Value
true
- Value is valid for this propertyfalse
- Value is not a valid value for this property
validateValue
validateValue(metaValidationObj,value)
Test if value is valid and throw error if it is not. This method throws an error
if value
is not a valid value for the property whose validation
is describe by metaValidationObj
. The error message is the same
as that thrown if the value is assigned to the property of an actual object.
Input Arguments
metaValidationObj
- Themeta.Validation
object for the propertyvalue
- The potential property value to test for validity
Return Value
none
Examples
The ValidationExample
class defines a property that used
validation.
classdef ValidationExample properties Prop (1,:) double {mustBeReal, mustBeGreaterThan(Prop, 10)} = 200; end end
The getErrorMessage
function determines if a potential value is
valid and displays the error message indicating the cause of invalid values.
function getErrorMessage(possibleValue) mc = ?ValidationExample; mp = findobj(mc.PropertyList,'Name','Prop'); mv = mp.Validation; if ~mv.isValidValue(possibleValue) try mv.validateValue(possibleValue) catch errorMessage fprintf('This value is not valid because: %s\n',... errorMessage.message); end else fprintf('%d is OK\n',possibleValue) end end
Version History
Introduced in R2018a