pip_services3_commons.validate.ObjectSchema module

pip_services3_commons.validate.ObjectSchema

Object schema implementation

copyright

Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.

license

MIT, see LICENSE for more details.

class pip_services3_commons.validate.ObjectSchema.ObjectSchema(allow_undefined: bool = False, required: bool = None, rules: List[pip_services3_commons.validate.IValidationRule.IValidationRule] = None)

Bases: pip_services3_commons.validate.Schema.Schema

Schema to validate user defined objects.

Example:

schema = ObjectSchema(false)
                    .with_optional_property("id", TypeCode.String)
                    .with_required_property("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
allow_undefined(value: bool)pip_services3_commons.validate.ObjectSchema.ObjectSchema

Sets flag to allow undefined properties

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

Parameters

value – true to allow undefined properties and false to disallow.

Returns

this validation schema.

get_properties() → List[pip_services3_commons.validate.PropertySchema.PropertySchema]

Gets validation schemas for object properties.

Returns

the list of property validation schemas.

property is_undefined_allowed

Gets flag to allow undefined properties

:return:true to allow undefined properties and false to disallow.

set_properties(value: List[pip_services3_commons.validate.PropertySchema.PropertySchema])

Sets validation schemas for object properties.

Parameters

value – a list of property validation schemas.

with_optional_property(name: str, typ: Optional[Any, None] = None, *rules: pip_services3_commons.validate.IValidationRule.IValidationRule)pip_services3_commons.validate.ObjectSchema.ObjectSchema

Adds a validation schema for an optional object property.

Parameters
  • name – a property name.

  • typ – (optional) a property schema or type.

  • rules – (optional) a list of property validation __rules.

Returns

the validation schema

with_property(schema: pip_services3_commons.validate.PropertySchema.PropertySchema)pip_services3_commons.validate.ObjectSchema.ObjectSchema

Adds a validation schema for an object property.

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

Parameters

schema – a property validation schema to be added.

Returns

this validation schema.

with_required_property(name: str, typ: Optional[Any, None] = None, *rules: pip_services3_commons.validate.IValidationRule.IValidationRule)pip_services3_commons.validate.ObjectSchema.ObjectSchema

Adds a validation schema for a __required object property.

Parameters
  • name – a property name.

  • typ – (optional) a property schema or type.

  • rules – (optional) a list of property validation __rules.

Returns

the validation schema