Inherits: 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. See IValidationRule
public
ObjectSchema()
Properties
IsUndefinedAllowed
Gets and sets a flag to allow undefined properties. True to allow undefined properties and false to disallow.
public
bool IsUndefinedAllowed { get; set; }
Properties
Gets and sets validation schemas for object properties. See PropertySchema
public
List<PropertySchema> Properties [get, set]
Instance methods
AllowUndefined
Sets a flag to allow undefined properties. This method returns a reference to this exception to implement the Builder pattern to chain additional calls.
public
ObjectSchema AllowUndefined(bool value)
- value: bool - true to allow undefined properties and false to disallow.
- returns: ObjectSchema - validation schema.
PerformValidation
Validates a given value against the schema and configured validation rules.
protected internal override
void PerformValidation(string path, object value, List<ValidationResult> results)
- path: string - dot notation path to the value.
- value: object - value to be validated.
- results: List<ValidationResult> - list with validation results to add new results.
WithOptionalProperty
Adds a validation schema for an optional object property.
public
ObjectSchema WithOptionalProperty(string name, object type, params IValidationRule[] rules)
- name: string - property name.
- type: object - (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
ObjectSchema WithProperty(PropertySchema schema)
- schema: PropertySchema - property validation schema to be added.
- returns: ObjectSchema - validation schema.
WithRequiredProperty
Adds a validation schema for a required object property.
public
ObjectSchema WithRequiredProperty(string name, object type, params IValidationRule[] rules)
- name: string - property name.
- type: object - (optional) property schema or type.
- rules: IValidationRule[] - (optional) list of property validation rules.
- returns: ObjectSchema - validation schema
Examples
var schema = new ObjectSchema().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