Schema to validate arrays.
Extends: Schema
Description
The ArraySchema class allows you to validate arrays based on a specified validation rule.
Constructors
Creates a new validation rule and sets its values. See TypeCode
public
constructor(valueType?: any, required?: boolean, rules?: IValidationRule[])
- valueType: any - a type of array elements. Null means that elements may have any type.
- required: boolean - (optional) true to always require non-null values.
- rules: IValidationRule[] - (optional) list with validation rules.
Instance methods
getValueType
Gets the type of array elements. Null means that elements may have any type.
public
getValueType(): any
- returns: any - type of array elements.
performValidation
Validates a given value against the schema and configured validation rules.
protected
performValidation(path: string, value: any, results: ValidationResult[]): void
- path: string - dot notation path to the value.
- value: any - value to be validated.
- results: ValidationResult[] - list with validation results.
setValueType
Sets the type of array elements. Null means that elements may have any type.
public
setValueType(value: any): void
- value: any - type of array elements.
Examples
let schema = new ArraySchema(TypeCode.String);
schema.validate(["A", "B", "C"]); // Result: no errors
schema.validate([1, 2, 3]); // Result: element type mismatch
schema.validate("A"); // Result: type mismatch