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

MapSchema([dynamic keyType, dynamic valueType, bool? req, List<IValidationRule> rules])

  • keyType: dynamic - type of map keys. Null means that keys may have any type.
  • valueType: dynamic - type of map values. Null means that values may have any type.
  • req: bool? - (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.

dynamic getKeyType()

  • returns: dynamic - type of map keys.

getValueType

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

dynamic getValueType()

  • returns: dynamic - type of map values.

performValidation

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

@override

void performValidation(String? path, dynamic value, List<ValidationResult> results)

  • path: String? - dot notation path to the value.
  • value: dynamic - value to be validated.
  • results: List<ValidationResult> - list with validation results to add new results.

setKeyType

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

void setKeyType(dynamic value)

  • value: dynamic - type of map keys.

setValueType

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

void setValueType(dynamic value)

  • value: dynamic - type of map values.

Examples

var schema =  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([ 1, 2, 3 ]);                        // Result: type mismatch