Skip to content
Advertisement

JOI validation for a string with json as a value

i am trying to validate the string using the JOI package available in npm, i checked this documentation which has many useful string formats e.g. date, IP, base64 but i need to validate the following JSON which contains a stringified JSON as a value, and there is no example in the documentation for such case

JavaScript

So for example what if i want to check engines have valid JSON value and have at-least one engine defined if infected key is set to true

The following schema works only if the engines value is written as parsed JSON

JavaScript

Advertisement

Answer

What you need to do is to create a custom JOI validator by extending the array validator of the JOI package and using that custom validator for the engines property.

JavaScript

You can see more examples like that here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement