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 undefines in the schema
  • required: boolean - (optional) true to always require non-null values.
  • rules: IValidationRule[] - (optional) a list with validation rules.

Properties

isUndefinedAllowed

Gets flag to allow undefined properties

public isUndefinedAllowed(): boolean

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

Sets flag to allow undefined properties

public isUndefinedAllowed(value: boolean): void

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

Instance methods

allowUndefined

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

public allowUndefined(value: boolean): ObjectSchema

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

getProperties

Gets validation schemas for object properties. See PropertySchema

public getProperties(): PropertySchema[]

  • returns: PropertySchema[] - the list of property validation schemas.

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.

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 - a property name.
  • type: any - (optional) a property schema or type.
  • rules: IValidationRule[] - (optional) a list of property validation rules.

withProperty

Adds a validation schema for an object property. This method returns reference to this exception to implement 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 - a property name.
  • type: any - (optional) a property schema or type.
  • rules: IValidationRule[] - (optional) a list of property validation rules.
  • returns: ObjectSchema - the 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