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

ObjectSchema([bool? allowUndefined, bool? req, List<IValidationRule>? rules])

  • allowUndefined: bool - true to allow properties undefined in the schema.
  • req: bool - (optional) true to always require non-null values.
  • rules: List<IValidationRule> - (optional) list with validation rules.

Properties

isUndefinedAllowed

Gets a flag used to allow undefined properties.

bool get isUndefinedAllowed

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

Sets a flag used to allow undefined properties.

set isUndefinedAllowed(bool value)

  • value: bool - 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.

ObjectSchema allowUndefined(bool value)

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

getProperties

Gets validation schemas for object properties.

See PropertySchema

List<PropertySchema> getProperties()

  • returns: List<PropertySchema> - list of property validation schemas.

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.

setProperties

Sets validation schemas for object properties.

See PropertySchema

void setProperties(List<PropertySchema> value)

withOptionalProperty

Adds a validation schema for an optional object property.

ObjectSchema withOptionalProperty(String name, [dynamic type, List<IValidationRule>? rules])

  • name: String - property name.
  • type: dynamic - (optional) property schema or type.
  • rules: List<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

dynamic withProperty(PropertySchema schema)

  • schema: PropertySchema - property validation schema to be added.
  • returns: dynamic - validation schema.

withRequiredProperty

Adds a validation schema for a required object property.

ObjectSchema withRequiredProperty(String name, [dynamic type, List<IValidationRule> rules])

  • name: String - property name.
  • type: dynamic - (optional) property schema or type.
  • rules: List<IValidationRule> - (optional) list of property validation rules.
  • returns: ObjectSchema - validation schema.

Examples

var schema =  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