MapSchema

Schema to validate maps.

Extends: Schema

Description

The MapSchema class provides you with a schema to validate maps

Constructors

Creates a new instance of validation schema and sets its values. See IValidationRule, TypeCode

public MapSchema(Object keyType, Object valueType, Boolean required, List<IValidationRule> rules)

  • keyType: Object - type of map keys. Null means that keys may have any type.
  • valueType: Object - type of map values. Null means that values may have any type.
  • required: Boolean - (optional) true to always require non-null values.
  • rules: List<IValidationRule> - (optional) list with validation rules.

Instance methods

getKeyType

Gets the type of map keys. Null means that keys may have any type.

public Object getKeyType()

  • returns: Object - type of map keys.

getValueType

Gets the type of map values. Null means that values may have any type.

public Object getValueType()

  • returns: Object - type of map values.

performValidation

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

protected 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 to add new results.

setKeyType

Sets the type of map keys. Null means that keys may have any type.

public void setKeyType(Object value)

  • value: Object - type of map keys.

setValueType

Sets the type of map values. Null means that values may have any type.

public void setValueType(Object value)

  • value: Object - type of map values.

Examples

{@code
  MapSchema schema = new MapSchema(TypeCode.String, TypeCode.Integer);
 
  schema.validate({ "key1": "A", "key2": "B" });       // Result: no errors
  schema.validate({ "key1": 1, "key2": 2 });           // Result: element type mismatch
  schema.validate(new int[]{ 1, 2, 3 });                        // Result: type mismatch
  }