Asserts the event's properties follow the constraints of a JSON schema.
One of the most common challenges when gathering data is agreement between all involved stakeholders on the format, structure, and semantics of data. One popular solution is to apply "data contracts" to ensure that different systems or components communicate effectively and accurately. This transformation uses JSON Schema specification to assert that event's properties obey the agreed constraints.
Events that fail to comply with the agreed constraint are currently dropped, but logic can be modified to re-route them to a DLQ-like destination.
Deployment
Add the contents of jsonschema.js in a library (instructions). IMPORTANT: Make sure that the name of the library is jsonschema.
Add the code from the code block to a new Javascript validation (instructions).
Make sure that the configuration matches your expectations (see following section).
Connect the new transformation to destination (instructions).
Notes/Troubleshooting
Only JSONSchema draft-2020-12 is currently supported.
Make sure that await is before the call to contracts.registerSchemaFrom....
Configuration
Register schemas for event
Schemas can be loaded either from URLs or from JSON objects. For example, the following validation function registers two schemas for two events from two different URLs:
export async function transformBatch(events, metadata) {
// Create a registry for schemas
const contracts = new Contracts();
// Register schema for event "Add To Cart" from a URL
await contracts.registerSchemaFromURL("Add To Cart", "https://raw.githubusercontent.com/ifoukarakis/tests/main/product.json");
// Register a different schema for event "User Registered" from a URL
await contracts.registerSchemaFromURL("User Registered", "https://raw.githubusercontent.com/ifoukarakis/tests/main/person.json");
// Register more events here.
return events.filter(event => contracts.validateProperties(event))
}
In the following example, a single schema is registered from a JSON Object:
Note: embedding the JSON schemas on the transformation's code should help improve performance, but might reduce readability of the code. Another approach would be to move schemas in to a separate library file.
Drop events in case there's no schema registered
Simply add false as an argument to Contracts constructor:
Thank you for contributing to RudderStack Transformations. Your submission will be reviewed soon.
Do follow the transformations-challenge channel on RudderStack slack community for updates on the challenge.
Contact Details
ioannis.foukarakis@gmail.com
Language
Javascript
Category
Data Security & Governance
Description
Properties Schema Validation
Description
Asserts the event's
properties
follow the constraints of a JSON schema.One of the most common challenges when gathering data is agreement between all involved stakeholders on the format, structure, and semantics of data. One popular solution is to apply "data contracts" to ensure that different systems or components communicate effectively and accurately. This transformation uses JSON Schema specification to assert that event's properties obey the agreed constraints.
Events that fail to comply with the agreed constraint are currently dropped, but logic can be modified to re-route them to a DLQ-like destination.
Deployment
jsonschema
.Notes/Troubleshooting
JSONSchema draft-2020-12
is currently supported.await
is before the call tocontracts.registerSchemaFrom...
.Configuration
Register schemas for event
Schemas can be loaded either from URLs or from JSON objects. For example, the following validation function registers two schemas for two events from two different URLs:
In the following example, a single schema is registered from a JSON Object:
Drop events in case there's no schema registered
Simply add
false
as an argument toContracts
constructor:const contracts = new Contracts(true);
Developing
A sample github project for writing transformations is available at https://github.com/ifoukarakis/rudderstack-transformations.
Code Block
Input Payload for testing
License