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).
Add the attribute
Section titled “Add the attribute”-
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.jsonNote its
rev. -
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}$" }] -
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.jsonThe realm answers with the saved type and a new
rev. If someone changed the type since you read it, it answers412: read it again and repeat. -
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" }'
Check it worked
Section titled “Check it worked”Find Maya by her number:
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.
Undo it
Section titled “Undo it”Read the type again, take employeeId out of schema.properties and policies, and save it with PUT and the new
rev.
Related
Section titled “Related”- Attributes and validation: every rule you can set.
- Sensitive attributes: keep a value out of every read.
- Search and paging: find users by any attribute.