Schema Validation Package
Schema Validation Package is a simple NestJS package (module) that allows you to validate your data against a schema which is get from a Schema Registry API and cached locally.
Features
- Validate incoming data against a schema. Uses AJV under the hood.
- Get schema from a Schema Registry API.
- Cache schema locally for faster access. It stores the cache entries for 5 minutes.
Getting Started
-
Add to your app.module.ts
the following import:
@Module({
imports: [
SchemaValidationModule,
],
controllers: [],
providers: [],
})
-
In your services or guards, use the SchemaValidationService
to validate your data against a schema.
How it works
The package is exposing a SchemaValidationModule
which is a NestJS module that should be imported in your application. This module provides a SchemaValidationService
which is a service that can be used to validate data against a schema.
SchemaValidationService
exposes a single method called validateAgainstSchema
with the following signature:
async validateAgainstSchema(schemaName: string, objectToValidate: unknown, schemaVersion?: string): { isValid: boolean, errors: Record<string, string> }
Required configuration
The package is using the following environment variables:
- SCHEMA_API_URL - URL of the Schema Registry API
- SCHEMA_API_SUBSCRIPTION_KEY - Subscription key for the Schema Registry API
- SCHEMA_VALIDATION_CACHE_TTL - TTL of the cached schema in milliseconds