PropertySchema

Schema to validate object properties

Extends: Schema

Description

The PropertySchema class allows you to create schemas to validate object properties.

Constructors

Creates a new validation rule and sets its arguments. See IValidationRule, TypeCode

public PropertySchema(String name, Object type, Boolean required, List<IValidationRule> rules)

  • name: String - (optional) property name
  • type: Object - (optional) property type
  • required: Boolean - (optional) true to always require non-null values.
  • rules: IValidationRule[] - (optional) list with validation rules.

Instance methods

getName

Gets the property name.

public String getName()

  • returns: String - property name.

getType

Gets the property type.

public Object getType()

  • returns: Object - property type.

performValidation

Validates a given value against the schema and configured validation rules.

public void performValidation(String path, Object value, List<ValidationResult> results)

  • path: String - dot notation path to the value.
  • value: Object - value to be validated.
  • results: ValidationResult[] - list with validation results.

setName

Sets the property name.

public void setName(String value)

  • value: String - new property name.

setType

Sets a new property type. The type can be defined as type, type name or TypeCode

public void setType(Object value)

  • value: Object - new property type.

Examples

{@code
  ObjectSchema schema = new ObjectSchema().withProperty(new PropertySchema("id", TypeCode.String));
 
  schema.validate(Map.of("id", "1", "name" ,"ABC" ));	// Result: no errors
  schema.validate(Map.of( "name", "ABC" ));                    // Result: no errors
  schema.validate(Map.of( "id", 1, "name", "ABC" ));	// Result: id type mismatch
  }

See also