Main Content

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

expand all

Class restriction applied to property, specified as a meta.class object. If the property definition does not contain class restriction, MATLAB® sets this property to a 0-by-0 meta.class object.

Attributes:

GetAccesspublic
SetAccessprivate

Dimensions of the property value, specified as a heterogeneous array of type meta.ArrayDimension or arrays of type meta.FixedDimension or meta.UnrestrictedDimension. If the property definition does not specify dimensions for the property, MATLAB sets this property to a 1-by-0 meta.ArrayDimension array.

Attributes:

GetAccesspublic
SetAccessprivate

Validation functions, specified as a cell array of function handles referencing each validation function. If the property does not use validation functions, MATLAB sets this property to a 1-by-0 cell array.

Attributes:

GetAccesspublic
SetAccessprivate

Attributes

Sealedtrue
HandleCompatibletrue

For information on class attributes, see Class Attributes.

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 - The meta.Validation object for the property

  • value - The potential property value to test for validity

Return Value

  • true - Value is valid for this property

  • false - 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 - The meta.Validation object for the property

  • value - 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
Introduced in R2018a