Compare 2 values in jsonfroms?

Hi, how can I compare values in jsonfroms ? I need 2 properties, for example “password” and “confirmPassword” to be equal and if they aren’t to display an error .

[original thread by PetarMalamov]

Hi @petarmalamov(petarmalamov), JSON Schema (Validation) itself is not powerful enough to formulate this constraint. However there are custom JSON Schema extensions which are also honored by Ajv, e.g. $data, with which this constraint could be checked.

However to not “pollute” the schema with this custom extension I would recommend creating a custom Ajv instance which can evaluate your password constraints and hand it over to JSON Forms.

Alternatively you could use a custom renderer which renders both the password and confirmPassword inputs and only updates the data object when both contain the same value. In this case you could also avoid explicitly modeling confirmPassword in the JSON Schema.


but if I use a custom renderer , will it be possible to return a custom error to say that the both fields aren’t equal ?

This depends on your needs. If you just want to show the error then this can be easily done as each renderer has full control over what is shown. However if you also want to emit the error via the change event then this can’t be done via the renderer alone but you also would need your own JsonFormsStateProvider. If that’s the case I would not recommend using this approach.


I have a save button ,that will open only if the schema is valid, so only displaying the error won’t work for me