Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sirraminyavari/ajv-ts-schema
Simplify AJV Schema Validation with TypeScript
https://github.com/sirraminyavari/ajv-ts-schema
ajv json-schema json-schema-validation json-schema-validator schema-validator ts-schema-validation typescript-ajv typescript-schema-validation validation validator
Last synced: about 24 hours ago
JSON representation
Simplify AJV Schema Validation with TypeScript
- Host: GitHub
- URL: https://github.com/sirraminyavari/ajv-ts-schema
- Owner: sirraminyavari
- License: apache-2.0
- Created: 2024-11-20T03:22:42.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2025-01-30T09:50:15.000Z (2 days ago)
- Last Synced: 2025-01-30T10:31:48.494Z (2 days ago)
- Topics: ajv, json-schema, json-schema-validation, json-schema-validator, schema-validator, ts-schema-validation, typescript-ajv, typescript-schema-validation, validation, validator
- Language: TypeScript
- Homepage: https://www.raminy.dev/article/18712bd0-e06d-80b2-8e76-f86720b48d01/Simplifying%20AJV%20Schema%20Validation%20with%20TypeScript
- Size: 220 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
A TypeScript-first approach to defining JSON Schemas for use with `AJV`.
## Overview
`AJV` is a powerful JSON Schema validator, but creating schemas directly in JavaScript or TypeScript can be verbose and error-prone. This package, `@raminyavari/ajv-ts-schema`, bridges the gap by allowing you to define schemas using TypeScript decorators, making your schemas type-safe and reusable.
## Features
- **Type-Safe Validation**: Leverages TypeScript for safer, cleaner schema definitions.
- **Decorator-Based**: Simplifies schema creation with class and property decorators.
- **AJV Integration**: Easily compile and validate schemas using `AJV`.
- **Schema Reusability**: Use defined schemas for validation, request processing, and more.## Installation
```bash
npm install @raminyavari/ajv-ts-schema
```Or if you use yarn:
```bash
yarn add @raminyavari/ajv-ts-schema
```Enable decorators in your `tsconfig.json`:
```json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
```_If you are a contributor and wnat to clone and run on your local_
Make sure `Husky` will run properly by running these commands from the root of the project:
```bash
chmod +x .husky/pre-commit
chmod +x .husky/_/husky.sh
```Then run:
```bash
yarn
yarn test # or 'yarn test:ui'
```## Quick Start
Define your schema using decorators:
```tsx
import { AjvObject, AjvProperty, AjvSchema } from "@raminyavari/ajv-ts-schema";@AjvObject()
class MySchema extends AjvSchema {
@AjvProperty({ type: "number", required: true })
foo!: number;@AjvProperty({ type: "string", nullable: true })
bar?: string | null;
}const schema = MySchema.getSchema();
```Validate with `AJV`:
```tsx
import Ajv from "ajv/dist/2020";
import addFormats from "ajv-formats";const ajv = new Ajv({ useDefaults: true });
addFormats(ajv);
const validate = ajv.compile(schema);if (!validate(input)) {
console.error(validate.errors);
}const typedSchema = AjvSchema.fromJson(MySchema, input);
```## Key Advantages
- **Code Completion**: Enhanced development experience with IDE suggestions.
- **Error Prevention**: Catch issues at compile-time rather than runtime.
- **Reusability**: Convert validated JSON objects into typed instances using `fromJson`.## Documentation
- Full documentation and examples: [Documentation](https://www.raminy.dev/article/18712bd0-e06d-80b2-8e76-f86720b48d01/Simplifying%20AJV%20Schema%20Validation%20with%20TypeScript)
- NPM Package: [@raminyavari/ajv-ts-schema](https://www.npmjs.com/package/@raminyavari/ajv-ts-schema)
- JSON Schema Reference: [AJV Docs](https://github.com/ajv-validator/ajv/blob/master/docs/json-schema.md)## Contributing
Contributions are welcome! Please submit issues or pull requests on the [GitHub Repository](https://github.com/sirraminyavari/ajv-ts-schema).