Class ObjectSchema
java.lang.Object
org.pipservices3.commons.validate.Schema
org.pipservices3.commons.validate.ObjectSchema
- Direct Known Subclasses:
PagingParamsSchema
Schema to validate user defined objects.
### Example ###
ObjectSchema 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
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionallowUndefined
(boolean value) Sets flag to allow undefined propertiesGets validation schemas for object properties.boolean
Gets flag to allow undefined propertiesprotected void
performValidation
(String path, Object value, List<ValidationResult> results) Validates a given value against the schema and configured validation rules.void
setProperties
(List<PropertySchema> value) Sets validation schemas for object properties.void
setUndefinedAllowed
(boolean value) Sets flag to allow undefined propertieswithOptionalProperty
(String name, Object type, IValidationRule... rules) Adds a validation schema for an optional object property.withProperty
(PropertySchema schema) Adds a validation schema for an object property.withRequiredProperty
(String name, Object type, IValidationRule... rules) Adds a validation schema for a required object property.Methods inherited from class org.pipservices3.commons.validate.Schema
getRules, isRequired, makeOptional, makeRequired, performTypeValidation, setRequired, setRules, validate, validateAndThrowException, validateAndThrowException, withRule
-
Constructor Details
-
ObjectSchema
public ObjectSchema()Creates a new validation schema and sets its values.
-
-
Method Details
-
getProperties
Gets validation schemas for object properties.- Returns:
- the list of property validation schemas.
- See Also:
-
setProperties
Sets validation schemas for object properties.- Parameters:
value
- a list of property validation schemas.- See Also:
-
isUndefinedAllowed
public boolean isUndefinedAllowed()Gets flag to allow undefined properties- Returns:
- true to allow undefined properties and false to disallow.
-
setUndefinedAllowed
public void setUndefinedAllowed(boolean value) Sets flag to allow undefined properties- Parameters:
value
- true to allow undefined properties and false to disallow.
-
allowUndefined
Sets flag to allow undefined propertiesThis method returns reference to this exception to implement Builder pattern to chain additional calls.
- Parameters:
value
- true to allow undefined properties and false to disallow.- Returns:
- this validation schema.
-
withProperty
Adds a validation schema for an object property.This method returns reference to this exception to implement Builder pattern to chain additional calls.
- Parameters:
schema
- a property validation schema to be added.- Returns:
- this validation schema.
- See Also:
-
withRequiredProperty
Adds a validation schema for a required object property.- Parameters:
name
- a property name.type
- (optional) a property schema or type.rules
- (optional) a list of property validation rules.- Returns:
- the validation schema
-
withOptionalProperty
Adds a validation schema for an optional object property.- Parameters:
name
- a property name.type
- (optional) a property schema or type.rules
- (optional) a list of property validation rules.- Returns:
- the validation schema
-
performValidation
Validates a given value against the schema and configured validation rules.- Overrides:
performValidation
in classSchema
- Parameters:
path
- a dot notation path to the value.value
- a value to be validated.results
- a list with validation results to add new results.
-