Class Schema

java.lang.Object
org.pipservices3.commons.validate.Schema
Direct Known Subclasses:
ArraySchema, MapSchema, ObjectSchema, PropertySchema

public class Schema extends Object
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 Details

    • Schema

      public Schema()
    • Schema

      public Schema(boolean required, List<IValidationRule> rules)
      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

      public List<IValidationRule> getRules()
      Gets validation rules to check values against.
      Returns:
      a list with validation rules.
    • setRules

      public void setRules(List<IValidationRule> value)
      Sets validation rules to check values against.
      Parameters:
      value - a list with validation rules.
    • makeRequired

      public Schema 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

      public Schema 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

      public 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.
      Parameters:
      rule - a validation rule to be added.
      Returns:
      this validation schema.
    • performValidation

      protected void performValidation(String path, Object value, List<ValidationResult> results)
      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 type
      value - a value to be validated.
      results - a list with validation results to add new results.
      See Also:
    • validate

      public List<ValidationResult> validate(Object value)
      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: