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 constructor(keyType?: any, valueType?: any, required?: boolean, rules?: IValidationRule[])

  • keyType: any - a type of map keys. Null means that keys may have any type.
  • valueType: any - a type of map values. Null means that values may have any type.
  • required: boolean - (optional) true to always require non-null values.
  • rules: IValidationRule[] - (optional) a list with validation rules.

Instance methods

getKeyType

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

public getKeyType(): any

  • returns: any - the type of map keys.

getValueType

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

public getValueType(): any

  • returns: any - the type of map values.

performValidation

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

protected performValidation(path: string, value: any, results: ValidationResult[]): void

  • path: string - a dot notation path to the value.
  • value: any - a value to be validated.
  • results: ValidationResult[] - a list with validation results to add new results.

setKeyType

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

public setKeyType(value: any): void

  • value: any - a type of map keys.

setValueType

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

public setValueType(value: any): void

  • value: any - a type of map values.

Examples

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