Lolo/Validate (JSON Schema Validator)
The Lolo/Validate Library Function
This library takes advantage of Ajv, Another JSON Schema Validator and it allows implementing complex data validation logic via declarative schemas for your JSON data, without writing code.
How To Use It
You first need an event object to validate, which you send from another function. That is, if you want to validate the body of a POST request coming from the HTTP trigger, you would need to validate ev.body. Please try the HTTP demo first to get up to speed on how to work with the event object from function to function.
To use the lolo/Validate library function you need to define your schema under Parameters, inside the function. As this library function will validate the incoming event object, you thus need to use a nested object in your schema when specifying a specific attribute that you want validated. If we follow the example from above, using the ev.body from a POST request, the appropriate JSON schema would use a nested object like the example you see below.
We are setting name as a required property in this example.
{
"type": "object",
"properties": {
"body": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
}
}
Use a JSON schema builder tool as a helper here to structure your JSON schema correctly.
What to Expect (i.e. Output)
If the event data bypasses successfully it will route the event object to the out port. If it throws an error, it will emit a response with the appropriate error message.
Youtube Video Tutorial with an HTTP Post Request
See a full demo below on how to use this to validate the body of a POST request with properties name and age.
Updated over 2 years ago