# Custom types

> Define object types of your own, such as a department or a cost center, and link them to your users and groups.

Some of what you keep isn't an attribute of a user. A department has its own name, budget code and members; a cost
center has its own owner; a project has its members. Define each as a **type** of your own, and its objects are
stored, checked, searched, linked and audited exactly as users and groups are, through the same calls. A custom type
belongs to your realm alone, and is ready to use the moment you define it.

## Before you begin

- **Privilege:** `RealmOwner`, or a privilege of your own over type definitions (`*REALM/entity/type/*`) and over the
  new type's objects (`*REALM/entity/department/*`). 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

A type is a JSON Schema document with four parts that matter: its `name`; its `category`, which for a type of your
own is `managed`; its `cardinality`, `collection` for a type that holds many objects or `singleton` for one; and its
`schema`, the attributes and links its objects carry. Its `policies` hold the rules every write must pass, as for
attributes: see [Attributes and validation](../attributes-and-validation/).

A **link** is an attribute marked with `x-relationship`: the type it points at, `one` or `many`, and the name of the
attribute on the other end that points back. Declare both ends, and a link made from either end shows on both.

```json
"members": { "type": "array",
             "x-relationship": { "target": "user", "cardinality": "many", "reverse": "departments" } }
```

## Set it up

1. Define the type. A department, with a budget code and its members:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/types/v1 \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "name": "department", "category": "managed", "cardinality": "collection",
       "description": "A department, with its budget code and its members",
       "schema": { "type": "object", "properties": {
         "displayName": { "type": "string" },
         "budgetCode":  { "type": "string" },
         "members": { "type": "array",
                      "x-relationship": { "target": "user", "cardinality": "many", "reverse": "departments" } }
       } },
       "policies": { "displayName": ["required"], "budgetCode": ["unique"] }
     }'
   ```

   The realm answers `201`.

2. Add the other end, `departments`, to your realm's user type, as
   [Attributes and validation](../attributes-and-validation/#set-it-up) describes:

   ```json
   "departments": { "type": "array",
                    "x-relationship": { "target": "department", "cardinality": "many", "reverse": "members" } }
   ```

3. Create a department, and add a member:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/department?id=finance" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "displayName": "Finance", "budgetCode": "FIN-100" }'

   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/department/finance/relationships/members \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H "If-Match: *" \
     -d '{ "targetId": "ben" }'
   ```

   Read Ben with `?expand=departments/*`, and the Finance department is there.

## Limits and defaults

| What | Default or limit |
|---|---|
| `category` | `managed` for a type of your own |
| `cardinality` | `collection` or `singleton`; fixed once the type exists |
| Types per realm, attributes per type | No fixed cap |
| A definition | About 2 MB |
| Links per object read with `expand` | At most 10,000 per link attribute |
| The same pair linked twice | Not refused unless the link attribute is marked `"unique": true`; then `409` |
| A `one` link already set | A second is refused `409` (`SINGLETON_EXISTS`); remove the first |

## When a save fails

| Answer | Code | What it means | What to do |
|---|---|---|---|
| `400` | `POLICY_VIOLATION` | The definition breaks a rule, such as a `category` other than `managed` | Use `managed` |
| `400` | `INVALID_REQUEST` | The definition is malformed, such as a link listed under `required` | Links can't be required; check the definition |
| `405` | `METHOD_NOT_ALLOWED` | A `PATCH` or `DELETE` on a type | Save the whole type with `PUT` |

## Audit record

Defining a type is recorded as `CREATED`, and each save as `UPDATED`, with the definition before and after. Objects
of the type are recorded like users and groups: see [The identity model](../identity-model/#audit-record).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A link shows on one end only | The other end isn't declared, or its `reverse` doesn't name this attribute | Declare both ends, each naming the other in `reverse` |
| A user is linked to the same department twice | Links aren't unique unless marked | Remove the extra link; mark the link attribute `"unique": true`, beside its `x-relationship` |

## Good to know

- **Types are never deleted.** A type you no longer need stays defined; a `DELETE` on it answers `405`.
- **A type's kind can't change.** `category` and `cardinality` are fixed once the type exists.
- **Members found by rule.** A type of your own can work out a link from a rule on users' attributes: see
  [Rule-based membership](../../groups/rule-based-membership/).
- **Sign users in against your own type.** See [Your own user type](../your-own-user-type/).

## Related

- [Attributes and validation](../attributes-and-validation/): add to the types you have.
- [The identity model](../identity-model/): types, links and revisions.
- [Rule-based membership](../../groups/rule-based-membership/): membership from a rule.
