Validation

You configure user data validation using the entityType.addRule API call. Use this call to apply rules to data added or updated in attributes.

You define rules by building a JSON object and using addRule to apply it to an attribute.  For example:

{"match-all": "[a-zA-Z]*"}, "attributes": ["/name"]}

Note: A rule can be either a string or an object. Strings are used when the argument type is none. Otherwise, an object is used, where the key is the name of the rule, and the value is the argument.

Violations:  if a rule is violated, a 360 (constraint_violation) error is returned. Error responses are explained in detail in the RESTful API topic.

Definition

The table below shows the available rules. Note that each works on a specific attribute data type. Some require arguments.

Name Transforms Input? Attribute Type Argument Type
match no string string
match-all no string string
min-length no string integer
max-length no string integer
less-than no integer number
greater-than no integer number
required no any none
min-age no date integer
default yes any integer/string
ignore-update yes any none
truncate yes string integer
to-lower yes string none
to-upper yes string none
and maybe array of rules array of strings and/or objects
or maybe array of rules array of strings and/or objects
not maybe rule string or object

String attributes filters

Integer attributes filters

Transforming attributes filters

Note: Filters that transform input are applied before filters that validate input.
  • truncate — Will take the first n characters of a string value, where n is an integer supplied in the definition.
  • to-lower or to-upper — Will convert a string value to lower (or upper) case. It takes no arguments.

Comparative filters

Value Filters

Attributes

Add the attribute paths to the attribute the filter will be applied to.

Examples

Here are some examples of rule definitions.

Values are truncated to 100 characters if their length is over 100, and converted to lower case.

{
  "and": [
      { "truncate": 100 }
    , "to-lower"
  ]
}

Values are rejected if they are less than six characters long, or contain any 3’s or f’s.

{
  "and": [
      { "min-length": 6 },
      { "not": {
            "match": "[3f]"
        }
      }
  ]
}

Set a minimum age of 16:

{
   "and":[
      {
         "min-age":16
      },
      "required"
   ]
}