1

Is there a way to make APIv4 validate options when creating/updating?

For example, using the demo data I can run:

$ cv api4 Contact.update '{"where":[["id","=",202]],"values":{"constituent_information.Marital_Status":"Junk"}}'

and then verify the update took place with:

$ cv api4 Contact.get '{"select":["constituent_information.Marital_Status"],"where":[["id","=",202]]}'

producing:

[
    {
        "id": 202,
        "constituent_information.Marital_Status": "Junk"
    }
]

So Marital_Status is set to Junk - even though that is not a valid option. When viewing this in the UI the field now shows as blank.

I could use getfields to get the valid options and check them before doing the API call but an option (or default behaviour) on the API call to produce some sort of error or warning would be useful.

1 Answer 1

1

APIv3 does option-matching by default. APIv4 only does option-matching if you ask it to. The way to do that is by specifying what property of the option you are passing in. Typically :name for the machine name or :label for the localized label.

Try this api call:

$ cv api4 Contact.update '{"where":[["id","=",202]],"values":{"constituent_information.Marital_Status:label":"Married"}}'

Passing in an invalid option should fail.

1
  • Thanks - so :name and :label are more than just convenience: they determine whether error-checking is applied. Using constituent_information.Marital_Status I can set any random invalid values, but using invalid values with :label does fail although with the rather cryptic "is not of type String".
    – Aidan
    Dec 7, 2020 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.