# Attributes and validation

> Add the attributes your organization keeps about users and groups, with the rules every write must pass.

Add an employee number, a department or a start date to your realm's `user` and `group` types, and they behave like
the shipped attributes: stored, searched, checked on every write, and shown in every read. Give each the rules it must
pass, and the realm refuses a bad value with every failure named at once.

## Before you begin

- **Privilege:** `RealmOwner`, or a privilege of your own over type definitions (`*REALM/entity/type/*` with
  `MODIFY`). Reading a type needs `RealmViewer`. See [Who may do what](../identity-model/#who-may-do-what).
- **Where:** the API, with your session in `$CVYUH_SESSION` (see [Call the API](../../../develop/api/)).

## How it works

Each realm has its own `user`, `group` and `agent` types, made from the shipped definitions when the realm was
created. You change your realm's copy by saving its definition again with what you've added.

The shipped attributes are marked `"x-carried": true`. Sign-in, tokens, the secrets store and the rest of the realm
rely on them, so you may change only their `default` and `description`. Everything else about them stays: you can't
remove one, change its kind, or drop its `required` entry or its rules. A save that tries is refused `400`
(`POLICY_VIOLATION`), naming the attribute. Everything you add beside them is yours to change or remove.

### Rules every write must pass

Each attribute can carry **policies**, checks run on every create and every change. A write that fails any of them
is refused `400` with every failure listed, each naming the attribute, the policy and what was wrong.

| Policy | What it checks |
|---|---|
| `required`, `not-null`, `not-empty` | The attribute is there, and not blank |
| `unique` | No other object of the type has the same value |
| `min-length`, `max-length` | Length, with `value` as the limit |
| `regex-match` | The value matches `pattern` |
| `valid-email`, `valid-phone`, `valid-name`, `valid-date` | The value is shaped like one |
| `valid-type` | The value's JSON type is one of `types` |
| `no-duplicates` | A list holds no value twice |
| `at-least-capitals`, `at-least-numbers`, `cannot-contain-characters`, `cannot-contain-value`, `cannot-contain-others` | Password strength, for a password attribute: see [Password rules](../../../workforce-identity/authentication/password-rules/) |

The schema's own keywords are checked too: `required`, `minLength`, `pattern`, `enum`, and the attribute's `type`.
**`format` is not checked:** a schema's `"format": "date-time"` or `"format": "email"` describes the value but doesn't
refuse a malformed one. To enforce a shape, use a policy: `valid-date`, `valid-email`, `valid-phone` or
`regex-match`.

## Set it up

1. Read your realm's type:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/types/v1/user \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   The answer is the whole definition, `name`, `category`, `cardinality`, `schema` and `policies`, with its `rev`.

2. Add your attributes under `schema.properties`, and any rules for them under `policies`, keeping everything that
   was there:

   ```json
   "employeeId": { "type": "string", "x-searchable": true },
   "department": { "type": "string" },
   "startDate":  { "type": "string", "format": "date" }
   ```

   ```json
   "policies": { "uid": ["unique", "required"],
                 "employeeId": ["unique", { "id": "regex-match", "pattern": "^E[0-9]{6}$" }],
                 "startDate": ["valid-date"] }
   ```

3. Save the whole definition back, naming the revision you read:

   ```sh
   curl -X PUT https://foobar.cvyuh.com/api/v0/am/types/v1/user \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H 'If-Match: "<rev>"' \
     -d @user-type.json
   ```

   `user-type.json` is the definition you read, with your additions. The realm answers with the saved type and its
   new `rev`. Without `If-Match` it answers `428`; if the type changed since you read it, `412`.

4. Write the new attributes like any other:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "employeeId": "E004211", "department": "Engineering" }'
   ```

## Limits and defaults

| What | Default or limit |
|---|---|
| Attributes per type | No fixed cap |
| Length of a value | No cap unless you set one: add `max-length` to every free-text attribute you add |
| A type's definition, or any request body | About 2 MB |
| Carried attributes | Only `default` and `description` can change |
| `valid-date` | An RFC 3339 time, a date (`2027-03-31`), or a date and time without an offset |

## When a save or a write fails

| Answer | Code | What it means | What to do |
|---|---|---|---|
| `400` | `POLICY_VIOLATION` | A save removed or changed a carried attribute, or a write broke a policy; each failure is named | Restore the carried attribute; fix the value |
| `400` | `SCHEMA_VALIDATION_FAILED` | A value's kind, `pattern`, `enum` or `minLength` failed | Fix the value |
| `400` | `INVALID_REQUEST` | The definition is malformed, such as a mark on the wrong kind of attribute | Check the attribute's definition |
| `412` | `REVISION_CONFLICT` | The type changed since you read it | Read it again, reapply your additions |
| `428` | `PRECONDITION_REQUIRED` | The save had no `If-Match` | Send the `rev` you read |

## Audit record

Each save of a type is recorded as `UPDATED`, with the definition before and after and who saved it; a new type as
`CREATED`. Writes of the attributes themselves are recorded on each object's
[change history](../identity-model/#every-change-is-recorded). See
[What is recorded](../../../audit-reports/audit-trail/what-is-recorded/).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A malformed date or email was stored | `format` describes, it doesn't check | Add `valid-date` or `valid-email` to the attribute's policies |
| An attribute you added earlier is gone | A `PUT` without it replaced the type | Add it back; always start from a fresh read |
| Stored objects break a new rule | Rules apply from the next write | Read them with a search and fix them, or let each next write catch them |
| A new shipped attribute isn't on your realm's type | Your realm's types are copies made when it was created | Add it yourself; see [What's next for attributes](../whats-next/) |

## Good to know

- **The body replaces the type.** A `PUT` that leaves out an attribute you added earlier removes it from the type.
  Always start from a fresh read.
- **Types are never deleted.** A `DELETE` on a type always answers `405`.
- **A type's kind can't change.** A change of `category` or `cardinality` is refused, since it would change what the
  objects already stored mean.
- **`x-searchable: true` indexes an attribute.** Every attribute can be filtered; mark the ones you search often.
- **Your realm's types are its own copies.** An improvement to the shipped `user`, `group` or `agent` doesn't reach a
  realm that already exists: see [What's next for attributes](../whats-next/).
- **New rules apply from the next write.** Objects already stored aren't checked again until they're next changed,
  and then the whole object must pass.

## Related

- [Add your own attribute to users](../../guides/add-an-attribute/): the task, start to finish.
- [Sensitive attributes](../sensitive-attributes/): keep a value out of every read.
- [Custom types](../custom-types/): when what you keep is an object of its own.
- [Search and paging](../../users/search/): find users by the attributes you add.
