How to automatically validate your input and output with JSONSchema
Nango automatically validates your integration inputs & outputs. It also offers ways to further customize data validation in code. The guide will walk you through each approach.
For more advanced use cases, you can generate your own validation schemas using the available files with the tool of your choice.
To create zod schemas you can use ts-to-zod npm package. This tool converts TypeScript definitions into Zod schemas, which can be used for runtime validation in your own Typescript codebase.
Copy
Ask AI
npx ts-to-zod .nango/schema.ts schema.zod.ts
You can use zod models in your scripts too
Copy
Ask AI
import { myZodModel } from '../../schema.zod.js';export default async function fetchData(nango: Nango) { const response = await nango.get({ endpoint: '/tickets' }); const isValid = myZodModel.parse(response.json); if (isValid) { [...] }}
To create zod schemas you can use ts-to-zod npm package. This tool converts TypeScript definitions into Zod schemas, which can be used for runtime validation in your own Typescript codebase.
Copy
Ask AI
npx ts-to-zod .nango/schema.ts schema.zod.ts
You can use zod models in your scripts too
Copy
Ask AI
import { myZodModel } from '../../schema.zod.js';export default async function fetchData(nango: Nango) { const response = await nango.get({ endpoint: '/tickets' }); const isValid = myZodModel.parse(response.json); if (isValid) { [...] }}
You can use go-jsonschema golang package. This tool converts JSON Schema definitions into Golang struct. Note that some syntax are not supported by this package.
Copy
Ask AI
go-jsonschema -p main .nango/schema.json > test.go
You can use typify rust package. This tool converts JSON Schema definitions into Rust types. Note that some syntax are not supported by this package.
JSON Schema is supported in most of the main software languages, here is a non-exhaustive list of how you can directly use this file to validate the records you receive from Nango.
Copy
Ask AI
import { Ajv } from 'ajv';import addFormats from 'ajv-formats';import jsonSchema from '.nango/schema.json';// Initiate AJVconst ajv = new Ajv({ allErrors: true, discriminator: true });addFormats(ajv);const modelToValidate = 'MyModelName';const myData = {"id": "hello-word"};// Compile the JSON schemaconst validate = ajv.compile({ ...jsonSchema, ...jsonSchema['definitions'][modelToValidate]});// Validate your datavalidate(myData);
Copy
Ask AI
import { Ajv } from 'ajv';import addFormats from 'ajv-formats';import jsonSchema from '.nango/schema.json';// Initiate AJVconst ajv = new Ajv({ allErrors: true, discriminator: true });addFormats(ajv);const modelToValidate = 'MyModelName';const myData = {"id": "hello-word"};// Compile the JSON schemaconst validate = ajv.compile({ ...jsonSchema, ...jsonSchema['definitions'][modelToValidate]});// Validate your datavalidate(myData);