Validate
The Validate node checks if the input data conforms to a specified JSON schema. It is useful for ensuring data integrity, validating user input, or enforcing a specific structure on data flowing through your spell.Inputs
schema(object, default:{}): The JSON schema to validate the data against. It should follow the JSON Schema specification.data(object, default:{}): The data object to be validated against the provided schema.
Outputs
result(boolean): Indicates whether the validation was successful. Returnstrueif the data is valid according to the schema, andfalseotherwise.errors(array): If the validation fails, this output provides an array of validation error messages describing the specific issues encountered.
Configuration
This node does not have any additional configuration options.Usage
- Connect a JSON schema object to the
schemainput. This schema should define the expected structure and constraints of the data you want to validate. - Connect the data object you want to validate to the
datainput. - The
resultoutput will indicate whether the validation was successful or not. - If the validation fails, the
errorsoutput will provide an array of error messages detailing the validation issues.
Example
Suppose you have a spell that processes user registration data. You want to ensure that the user input follows a specific structure before proceeding with the registration. Here’s how you can use the Validate node:-
Define a JSON schema for the user registration data:
-
Connect this schema to the
schemainput of the Validate node. -
Connect the user registration data (e.g., from a form submission) to the
datainput of the Validate node. -
The
resultoutput will betrueif the user data is valid according to the schema, andfalseotherwise. -
If the validation fails, the
errorsoutput will provide an array of error messages, such as: -
Use a Branch node to handle the validation result. If
resultistrue, proceed with the registration process. Ifresultisfalse, handle the validation errors (e.g., display error messages to the user).
Best Practices
- Ensure that your JSON schema accurately represents the expected structure and constraints of your data.
- Use descriptive error messages in your schema to help identify and troubleshoot validation issues easily.
- Handle validation errors gracefully in your spell, providing clear feedback to users when their input is invalid.
Common Issues
- Make sure the
schemainput is a valid JSON schema object. Incorrect schema syntax can lead to unexpected validation results. - Be cautious when defining complex schemas with nested objects or arrays. Test your schema thoroughly to ensure it validates data correctly.
- If the
datainput is not a valid JSON object, the validation may fail with a parsing error.
