ObjectSchema

Schema to validate user defined objects.

Extends: Schema

Description

The ObjectSchema class allows you to create a validation schema that can be used to validate user defined objects.

Constructors

Creates a new validation schema and sets its values. See IValidationRule

public constructor(allowUndefined?: boolean, required?: boolean, rules?: IValidationRule[])

  • allowUndefined: boolean - true to allow properties undefined in the schema.
  • required: boolean - (optional) true to always require non-null values.
  • rules: IValidationRule[] - (optional) list with validation rules.

Properties

isUndefinedAllowed

Gets a flag used to allow undefined properties.

public isUndefinedAllowed(): boolean

  • returns: boolean - true to allow undefined properties and false to disallow.

Sets a flag used to allow undefined properties.

public isUndefinedAllowed(value: boolean): void

  • value: boolean - true to allow undefined properties and false to disallow.

Instance methods

allowUndefined

Sets a flag used to allow undefined properties. This method returns a reference to this exception to implement the Builder pattern to chain additional calls.

public allowUndefined(value: boolean): ObjectSchema

  • value: boolean - true to allow undefined properties and false to disallow.
  • returns: ObjectSchema - validation schema.

getProperties

Gets validation schemas for object properties. See PropertySchema

public getProperties(): PropertySchema[]

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.

setProperties

Sets validation schemas for object properties. See PropertySchema

public setProperties(value: PropertySchema[]): void

withOptionalProperty

Adds a validation schema for an optional object property.

public withOptionalProperty(name: string, type?: any, …rules: IValidationRule[]): ObjectSchema

  • name: string - property name.
  • type: any - (optional) property schema or type.
  • rules: IValidationRule[] - (optional) list of property validation rules.
  • returns: ObjectSchema - returns Schema with added optional property

withProperty

Adds a validation schema for an object property. This method returns a reference to this exception to implement the Builder pattern to chain additional calls. See PropertySchema

public withProperty(schema: PropertySchema): ObjectSchema

withRequiredProperty

Adds a validation schema for a required object property.

public withRequiredProperty(name: string, type?: any, …rules: IValidationRule[]): ObjectSchema

  • name: string - property name.
  • type: any - (optional) property schema or type.
  • rules: IValidationRule[] - (optional) list of property validation rules.
  • returns: ObjectSchema - validation schema.

Examples

let schema = new ObjectSchema(false)
    .withOptionalProperty("id", TypeCode.String)
    .withRequiredProperty("name", TypeCode.String);
  
schema.validate({ id: "1", name: "ABC" });       // Result: no errors
schema.validate({ name: "ABC" });                // Result: no errors
schema.validate({ id: 1, name: "ABC" });         // Result: id type mismatch
schema.validate({ id: 1, _name: "ABC" });        // Result: name is missing, unexpected _name
schema.validate("ABC");                          // Result: type mismatch