Pip-Services-Commons-Dotnet
Public Member Functions | Properties | List of all members
PipServices3.Commons.Validate.ObjectSchema Class Reference

Schema to validate user defined objects. More...

Inheritance diagram for PipServices3.Commons.Validate.ObjectSchema:
PipServices3.Commons.Validate.Schema PipServices3.Commons.Validate.PagingParamsSchema PipServices3.Commons.Validate.SortFieldSchema

Public Member Functions

ObjectSchema AllowUndefined (bool value)
 Sets flag to allow undefined properties. This method returns reference to this exception to implement Builder pattern to chain additional calls. More...
 
ObjectSchema WithProperty (PropertySchema schema)
 Adds a validation schema for an object property. More...
 
ObjectSchema WithRequiredProperty (string name, object type, params IValidationRule[] rules)
 Adds a validation schema for a required object property. More...
 
ObjectSchema WithOptionalProperty (string name, object type, params IValidationRule[] rules)
 Adds a validation schema for an optional object property. More...
 
- Public Member Functions inherited from PipServices3.Commons.Validate.Schema
 Schema ()
 Creates a new instance of validation schema. More...
 
 Schema (bool required, List< IValidationRule > rules)
 Creates a new instance of validation schema and sets its values. More...
 
Schema MakeRequired ()
 Makes validated values always required (non-null). For null values the schema will raise errors. More...
 
Schema MakeOptional ()
 Makes validated values optional. Validation for null values will be skipped. More...
 
Schema WithRule (IValidationRule rule)
 Adds validation rule to this schema. This method returns reference to this exception to implement Builder pattern to chain additional calls. More...
 
List< ValidationResultValidate (object value)
 Validates the given value and results validation results. More...
 
void ValidateAndThrowException (string correlationId, object value, bool strict=false)
 Validates the given value and returns a ValidationException if errors were found. More...
 

Properties

List< PropertySchemaProperties [get, set]
 
bool IsUndefinedAllowed [get, set]
 
- Properties inherited from PipServices3.Commons.Validate.Schema
bool IsRequired [get, set]
 
List< IValidationRuleRules [get, set]
 

Additional Inherited Members

- Protected Member Functions inherited from PipServices3.Commons.Validate.Schema
void PerformTypeValidation (string path, object type, object value, List< ValidationResult > results)
 Validates a given value to match specified type. The type can be defined as a Schema, type, a type name or TypeCode When type is a Schema, it executes validation recursively against that Schema. More...
 

Detailed Description

Schema to validate user defined objects.

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

Member Function Documentation

◆ AllowUndefined()

ObjectSchema PipServices3.Commons.Validate.ObjectSchema.AllowUndefined ( bool  value)

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

Parameters
valuetrue to allow undefined properties and false to disallow.
Returns
this validation schema.

◆ WithOptionalProperty()

ObjectSchema PipServices3.Commons.Validate.ObjectSchema.WithOptionalProperty ( string  name,
object  type,
params IValidationRule []  rules 
)

Adds a validation schema for an optional object property.

Parameters
namea property name.
type(optional) a property schema or type.
rules(optional) a list of property validation rules.
Returns
the validation schema

◆ WithProperty()

ObjectSchema PipServices3.Commons.Validate.ObjectSchema.WithProperty ( PropertySchema  schema)

Adds a validation schema for an object property.

This method returns reference to this exception to implement Builder pattern to chain additional calls.

Parameters
schemaa property validation schema to be added.
Returns
this validation schema.

See PropertySchema

◆ WithRequiredProperty()

ObjectSchema PipServices3.Commons.Validate.ObjectSchema.WithRequiredProperty ( string  name,
object  type,
params IValidationRule []  rules 
)

Adds a validation schema for a required object property.

Parameters
namea property name.
type(optional) a property schema or type.
rules(optional) a list of property validation rules.
Returns
the validation schema

The documentation for this class was generated from the following file: