Typical Validation Use Cases

This document describes best practices and typical use cases for writing graph validation rules. These examples specify validation rules that can be copy/pasted into a schema.

Since most graph fields allow either a value to be entered or a link to be connected, the most common rule is a compound “oneOf” rule that includes both a “type” rule and a “numLinks” rule. The following examples mostly follow this pattern.

String field with minimum and maximum lengths

The following validation rule specifies that a field must be a string with a minimum length of 1 character and a maximum length of 20 characters or it must be connected to exactly one link that will provide the value.

validation:
  oneOf:
    - type: string
      minLength: 1
      maxLength: 20
    - numLinks: one

Number field with minimum and maximum values

The following validation rule specifies that a field must be a number with a value between 0-1 or it must be connected to exactly one link that will provide the value.

validation:
  oneOf:
    - type: number
      minimum: 0
      maximum: 1.0
    - numLinks: one

Integer field with minimum and maximum values

The following validation rule specifies that a field must be an integer with a value between 0-255 or it must be connected to exactly one link that will provide the value.

validation:
  oneOf:
    - type: integer
      minimum: 0
      maximum: 255
    - numLinks: one

The following validation rule specifies that the field must be connected to exactly one link. A value cannot be entered for the field.

validation:
  numLinks: one

The following validation rule specifies that the field must be connected to exactly one or more links. A value cannot be entered for the field.

validation:
  numLinks: oneOrMany

Fixed length array of integer values

The following validation rule specifies that the field must contain an array of exactly three elements, each of which must be an integer in the range 0-255. Alternatively, the field may be connected to a link that will provide the array.

validation:
  oneOf:
    - type: array
      items:
        type: integer
        minimum: 0
        maximum: 255
      minItems: 3
      maxItems: 3
    - numLinks: one

Fixed length array with different element types

The following validation rule is for an array that contains three integers ranging 0-255, and an fourth value which is a number ranging from 0-1.0. Alternatively, the field may be connected to a link that will provide the array.

validation:
  oneOf:
    - type: array
      prefixItems:
      - type: integer
        minimum: 0
        maximum: 255
      - type: integer
        minimum: 0
        maximum: 255
      - type: integer
        minimum: 0
        maximum: 255
      - type: number
        minimum: 0
        maximum: 1.0
    - numLinks: one

Last updated