@nesn/commons-schema-validator
v1.0.1
Published
The `@nesn/commons-shared-objects-schema-validator` module provides a validation utility for ensuring data adherence to defined JSON schemas. This utility is particularly useful when working with data structures like the `NormalizedSubscription` interface
Downloads
22
Readme
Schema Validator Module Readme
The @nesn/commons-shared-objects-schema-validator
module provides a validation utility for ensuring data adherence to defined JSON schemas. This utility is particularly useful when working with data structures like the NormalizedSubscription
interface provided by the NormalizedSubscription
module. This readme provides an overview of the module's purpose, usage, and integration.
Table of Contents
Introduction
The @nesn/commons-shared-objects-schema-validator
module aims to streamline the validation of data against JSON schemas. By leveraging the Ajv library, it ensures that data structures conform to specified schemas, enabling reliable and consistent data processing. This module can be integrated with any codebase that requires schema-based validation.
Installation
To include the @nesn/commons-shared-objects-schema-validator
module in your project, you can install it using npm:
npm install @nesn/commons-shared-objects-schema-validator
Usage
Importing
Import the validateObjectAgainstSchema
function from the module:
import { validateObjectAgainstSchema } from '@nesn/commons-shared-objects-schema-validator';
Validation
The validateObjectAgainstSchema
function takes two parameters: the data object to be validated and the JSON schema against which the data will be validated. If the data adheres to the schema, the function returns true
. If not, it throws a SchemaValidationError
.
Here's an example of using the validator with the NormalizedSubscription
interface and schema:
import { validateObjectAgainstSchema } from '@nesn/commons-shared-objects-schema-validator';
import { NormalizedSubscription } from '@nesn/commons-shared-objects-normalized-subscription';
const subscriptionData = /* your subscription data */;
try {
validateObjectAgainstSchema(subscriptionData, NormalizedSubscription);
console.log('Data is valid!');
} catch (error) {
if (error instanceof SchemaValidationError) {
console.error('Validation error:', error.message);
// Handle validation errors
} else {
// Handle other errors
}
}
Customization
The @nesn/commons-shared-objects-schema-validator
module provides a default SchemaValidationError
class for handling validation errors. You can extend or modify this class to suit your project's needs. This might include additional error information or custom error messages.
Contributing
Contributions to the @nesn/commons-shared-objects-schema-validator
module are welcome. If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.
License
The @nesn/commons-shared-objects-schema-validator
module is released under the MIT License. You can find the full license text in the module's package.json
file.
Please note that this readme is provided as an example and might need adjustments based on your specific project and documentation guidelines.