# 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 <a href="#string-field-with-minimum-and-maximum-lengths" id="string-field-with-minimum-and-maximum-lengths"></a>

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.

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

### Number field with minimum and maximum values <a href="#number-field-with-minimum-and-maximum-values" id="number-field-with-minimum-and-maximum-values"></a>

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.

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

### Integer field with minimum and maximum values <a href="#integer-field-with-minimum-and-maximum-values" id="integer-field-with-minimum-and-maximum-values"></a>

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.

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

### Field that takes exactly one link <a href="#field-that-takes-exactly-one-link" id="field-that-takes-exactly-one-link"></a>

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

```yaml
validation:
  numLinks: one
```

### Field that takes one or more links <a href="#field-that-takes-one-or-more-links" id="field-that-takes-one-or-more-links"></a>

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.

```yaml
validation:
  numLinks: oneOrMany
```

### Fixed length array of integer values <a href="#fixed-length-array-of-integer-values" id="fixed-length-array-of-integer-values"></a>

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.

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

### Fixed length array with different element types <a href="#fixed-length-array-with-different-element-types" id="fixed-length-array-with-different-element-types"></a>

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.

```yaml
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
```

&#x20;
