The goal of @mdf.js/doorkeeper is to provide a simple and robust solution for validating, registering, and managing JSON schemas in diverse applications. The code is designed to leverage advanced JSON schema validation using AJV (Another JSON Schema Validator), enriched with additional functionalities.
- npm
npm install @mdf.js/doorkeeper
- yarn
yarn add @mdf.js/doorkeeper
This package is part of the @mdf.js project, a collection of packages for building applications with Node.js and Typescript.
@mdf.js/doorkeeper has been designed to store and manage all the JSON schemas used in an application, allowing to assign a unique identifier to each schema, which it is associated with concrete type or interface, in this way, it is possible to validate the data of the application in a simple and robust way and to obtain the type of the data for Typescript applications.
import { Doorkeeper } from '@mdf.js/doorkeeper';
export interface User {
name: string;
age: number;
};
export interface Address {
street: string;
city: string;
country: string;
};
const userSchema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
},
required: ['name', 'age'],
additionalProperties: false,
};
const addressSchema = {
type: 'object',
properties: {
street: { type: 'string' },
city: { type: 'string' },
country: { type: 'string' },
},
required: ['street', 'city', 'country'],
additionalProperties: false,
};
export interface Schemas {
'User': User;
'Address': Address;
}
const checker = new Doorkeeper<Schemas>().register({
'User': userSchema,
'Address': addressSchema,
});
const user: User = {
name: 'John',
age: 30,
};
const address: Address = {
street: 'Main Street',
city: 'New York',
country: 'USA',
};
const myNewUser = await checker.validate('User', user); // myNewUser is of type User
const myNewAddress = await checker.validate('Address', address); // myNewAddress is of type Address
- {@link DoorKeeper}
Copyright 2024 Mytra Control S.L. All rights reserved.
Use of this source code is governed by an MIT-style license that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.