Main Content

matlab.metadata.Validation Class

Namespace: matlab.metadata

Describes property validation

Renamed from meta.Validation in R2024a

Description

Instances of this class contain information about property validation that is specified in a class definition. The matlab.metadata.Validation class enables you to obtain 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.

Class Attributes

Sealedtrue

For information on class attributes, see Class Attributes.

Properties

expand all

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

Attributes:

GetAccesspublic
SetAccessprivate

Dimensions of the property value, specified as a heterogeneous array of type matlab.metadata.ArrayDimension or arrays of type matlab.metadata.FixedDimension or matlab.metadata.UnrestrictedDimension. If the property definition does not specify dimensions for the property, MATLAB sets this property to a 1-by-0 matlab.metadata.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

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 described by metaValidationObj.

Input Arguments

  • metaValidationObj - The matlab.metadata.Validation object for the property

  • value - The potential property value to test for validity

Output Argument

  • 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 described 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 matlab.metadata.Validation object for the property

  • value - The potential property value to test for validity

Examples

The ValidationExample class defines a property that uses several types of validation.

classdef ValidationExample
   properties
      Prop (1,:) double {mustBeReal,mustBeGreaterThan(Prop,10)} = 200;
   end
end

The getErrorMessage function determines if a potential value of Prop is valid. If the value is not valid for one or more reasons, the function displays an error message describing the first violation it finds.

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

For example, test a 2-by-2 matrix as a possible value. getErrorMessage returns an error because the input is not a vector.

getErrorMessage([11 3; 22 50])
This value is not valid because: Value must be a vector.

Changing the input to a vector corrects this problem, but the value is still not valid. The second element of the vector is not greater than 10, as required by the mustBeGreaterThan validation function.

getErrorMessage([11 3 22 50])
This value is not valid because: Value must be greater than 10.

Version History

Introduced in R2018a

expand all