Class Schema
java.lang.Object
org.pipservices3.commons.validate.Schema
- Direct Known Subclasses:
ArraySchema
,MapSchema
,ObjectSchema
,PropertySchema
Basic schema that validates values against a set of validation rules.
This schema is used as a basis for specific schemas to validate objects, project properties, arrays and maps.
- See Also:
-
Constructor Summary
ConstructorDescriptionSchema()
Schema
(boolean required, List<IValidationRule> rules) Creates a new instance of validation schema and sets its values. -
Method Summary
Modifier and TypeMethodDescriptiongetRules()
Gets validation rules to check values against.boolean
Gets a flag that always requires non-null values.Makes validated values optional.Makes validated values always required (non-null).protected void
performTypeValidation
(String path, Object type, Object value, List<ValidationResult> results) Validates a given value to match specified type.protected void
performValidation
(String path, Object value, List<ValidationResult> results) Validates a given value against the schema and configured validation rules.void
setRequired
(boolean value) Sets a flag that always requires non-null values.void
setRules
(List<IValidationRule> value) Sets validation rules to check values against.Validates the given value and results validation results.void
validateAndThrowException
(String correlationId, Object value) Validates the given value and throws a ValidationException if errors were found.void
validateAndThrowException
(String correlationId, Object value, boolean strict) Validates the given value and returns a ValidationException if errors were found.withRule
(IValidationRule rule) Adds validation rule to this schema.
-
Constructor Details
-
Schema
public Schema() -
Schema
Creates a new instance of validation schema and sets its values.- Parameters:
required
- (optional) true to always require non-null values.rules
- (optional) a list with validation rules.- See Also:
-
-
Method Details
-
isRequired
public boolean isRequired()Gets a flag that always requires non-null values. For null values it raises a validation error.- Returns:
- true to always require non-null values and false to allow null values.
-
setRequired
public void setRequired(boolean value) Sets a flag that always requires non-null values.- Parameters:
value
- true to always require non-null values and false to allow null values.
-
getRules
Gets validation rules to check values against.- Returns:
- a list with validation rules.
-
setRules
Sets validation rules to check values against.- Parameters:
value
- a list with validation rules.
-
makeRequired
Makes validated values always required (non-null). For null values the schema will raise errors. This method returns reference to this exception to implement Builder pattern to chain additional calls.- Returns:
- this validation schema
- See Also:
-
makeOptional
Makes validated values optional. Validation for null values will be skipped. This method returns reference to this exception to implement Builder pattern to chain additional calls.- Returns:
- this validation schema
- See Also:
-
withRule
Adds validation rule to this schema. This method returns reference to this exception to implement Builder pattern to chain additional calls.- Parameters:
rule
- a validation rule to be added.- Returns:
- this validation schema.
-
performValidation
Validates a given value against the schema and configured validation rules.- 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.
-
performTypeValidation
protected 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.- Parameters:
path
- a dot notation path to the value.type
- a type to match the value typevalue
- a value to be validated.results
- a list with validation results to add new results.- See Also:
-
validate
Validates the given value and results validation results.- Parameters:
value
- a value to be validated.- Returns:
- a list with validation results.
- See Also:
-
validateAndThrowException
public void validateAndThrowException(String correlationId, Object value, boolean strict) throws ValidationException Validates the given value and returns a ValidationException if errors were found.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.value
- a value to be validated.strict
- true to treat warnings as errors.- Throws:
ValidationException
- when errors occured in validation
-
validateAndThrowException
public void validateAndThrowException(String correlationId, Object value) throws ValidationException Validates the given value and throws a ValidationException if errors were found.- Parameters:
correlationId
- (optional) transaction id to trace execution through call chain.value
- a value to be validated.- Throws:
ValidationException
- when errors occured in validation- See Also:
-