Class ObjectSchema

java.lang.Object
org.pipservices3.commons.validate.Schema
org.pipservices3.commons.validate.ObjectSchema
Direct Known Subclasses:
PagingParamsSchema

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

    • ObjectSchema

      public ObjectSchema()
      Creates a new validation schema and sets its values.
  • Method Details

    • getProperties

      public List<PropertySchema> getProperties()
      Gets validation schemas for object properties.
      Returns:
      the list of property validation schemas.
      See Also:
    • setProperties

      public void setProperties(List<PropertySchema> value)
      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

      public ObjectSchema allowUndefined(boolean value)
      Sets flag to allow undefined properties

      This 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

      public 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:
      schema - a property validation schema to be added.
      Returns:
      this validation schema.
      See Also:
    • withRequiredProperty

      public ObjectSchema withRequiredProperty(String name, Object type, IValidationRule... rules)
      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

      public ObjectSchema withOptionalProperty(String name, Object type, IValidationRule... rules)
      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

      protected void performValidation(String path, Object value, List<ValidationResult> results)
      Validates a given value against the schema and configured validation rules.
      Overrides:
      performValidation in class Schema
      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.