Skip to content
Directory & LifecycleMarkdown

Add your own attribute to users

Keep an employee number on every user, checked for shape and uniqueness, and find users by it.

API

Your HR system names every employee by a number like E004211, and your apps want it on the user. In this guide you add employeeId to your realm’s user type, make the realm refuse a malformed or duplicate one, set it on a user, and find them by it.

Before you start: you’re a realm administrator holding RealmOwner, with your session in $CVYUH_SESSION (see Call the API).

  1. Read your realm’s user type into a file:

    Terminal window
    curl https://foobar.cvyuh.com/api/v0/am/types/v1/user \
    -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
    -o user-type.json

    Note its rev.

  2. In the file’s schema.properties, add the attribute, marked for search since you’ll look users up by it:

    "employeeId": { "type": "string", "x-searchable": true }

    In its policies, add the rules, keeping the ones already there:

    "employeeId": ["unique", { "id": "regex-match", "pattern": "^E[0-9]{6}$" }]
  3. Save the edited definition back:

    Terminal window
    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

    The realm answers with the saved type and a new rev. If someone changed the type since you read it, it answers 412: read it again and repeat.

  4. Set it on a user:

    Terminal window
    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" }'

Find Maya by her number:

Terminal window
curl -G https://foobar.cvyuh.com/api/v0/am/objects/v1/user \
-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
--data-urlencode 'filter={"filter":{"field":"employeeId","op":"=","value":"E004211"}}'

Then try a bad one, { "employeeId": "4211" }: the realm refuses it 400, naming employeeId and the regex-match policy. Give Ben E004211 too, and it’s refused as not unique.

Read the type again, take employeeId out of schema.properties and policies, and save it with PUT and the new rev.