Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Zod | 20,282 | 855 | 6 hours ago | 244 | September 11, 2022 | 172 | mit | TypeScript | ||
TypeScript-first schema validation with static type inference | ||||||||||
Yup | 19,999 | 8,912 | 2,408 | 4 hours ago | 115 | August 20, 2022 | 167 | mit | TypeScript | |
Dead simple Object schema validation | ||||||||||
Ajv | 12,307 | 801,606 | 7,985 | 2 days ago | 349 | March 22, 2022 | 252 | mit | TypeScript | |
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927) | ||||||||||
Formily | 9,334 | 119 | 2 days ago | 209 | September 20, 2022 | 11 | mit | TypeScript | ||
📱🚀 🧩 Cross Device & High Performance Normal Form/Dynamic(JSON Schema) Form/Form Builder -- Support React/React Native/Vue 2/Vue 3 | ||||||||||
Marshmallow | 6,465 | 9,091 | 969 | 6 days ago | 168 | June 26, 2022 | 162 | mit | Python | |
A lightweight library for converting complex objects to and from simple Python datatypes. | ||||||||||
Superstruct | 6,417 | 348 | 296 | 23 days ago | 75 | September 22, 2022 | 75 | mit | TypeScript | |
A simple and composable way to validate data in JavaScript (and TypeScript). | ||||||||||
Jsonschema | 4,069 | 15,530 | 2,424 | 3 days ago | 74 | July 07, 2022 | 29 | mit | Python | |
An implementation of the JSON Schema specification for Python | ||||||||||
Json Schema | 3,344 | 8,501 | 489 | 3 months ago | 46 | April 13, 2022 | 96 | mit | PHP | |
PHP implementation of JSON schema. Fork of the http://jsonschemaphpv.sourceforge.net/ project | ||||||||||
Kubeval | 3,043 | 8 | 20 | 17 days ago | 13 | April 26, 2021 | 104 | other | Go | |
Validate your Kubernetes configuration files, supports multiple Kubernetes versions | ||||||||||
Json Schema Spec | 2,834 | 18 hours ago | 105 | other | Makefile | |||||
The JSON Schema I-D sources |
This lib allows you to apply Joi validation constraints on class properties, by using decorators.
This means you can combine your type schema and your validation schema in one go!
Calling Validator.validateAsClass()
allows you to validate any object as if it were an instance of a given class.
npm add joiful reflect-metadata
Or
yarn add joiful reflect-metadata
.
You must enable experimental decorators and metadata in your TypeScript configuration.
tsconfig.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
Ensure you import reflect-metadata
as the first import in your application's entry point.
index.ts
import 'reflect-metadata';
...
Then you can start using joiful like this.
import * as jf from 'joiful';
class SignUp {
@jf.string().required()
username: string;
@jf
.string()
.required()
.min(8)
password: string;
@jf.date()
dateOfBirth: Date;
@jf.boolean().required()
subscribedToNewsletter: boolean;
}
const signUp = new SignUp();
signUp.username = 'rick.sanchez';
signUp.password = 'wubbalubbadubdub';
const { error } = jf.validate(signUp);
console.log(error); // Error will either be undefined or a standard joi validation error
Don't like creating instances of classes? Don't worry, you don't have to. You can validate a plain old javascript object as if it were an instance of a class.
const signUp = {
username: 'rick.sanchez',
password: 'wubbalubbadubdub',
};
const result = jf.validateAsClass(signUp, SignUp);
Want to create your own shorthand versions of decorators? Simply create a function like below.
customDecorators.ts
import * as jf from 'joiful';
const password = () =>
jf
.string()
.min(8)
.regex(/[a-z]/)
.regex(/[A-Z]/)
.regex(/[0-9]/)
.required();
changePassword.ts
import { password } from './customDecorators';
class ChangePassword {
@password()
newPassword: string;
}
class SimpleTodoList {
@jf.array().items(joi => joi.string())
todos?: string[];
}
To validate an array of objects that have their own joiful validation:
class Actor {
@string().required()
name!: string;
}
class Movie {
@string().required()
name!: string;
@array({ elementClass: Actor }).required()
actors!: Actor[];
}
To validate an object subproperty that has its own joiful validation:
class Address {
@string()
line1?: string;
@string()
line2?: string;
@string().required()
city!: string;
@string().required()
state!: string;
@string().required()
country!: string;
}
class Contact {
@string().required()
name!: string;
@object().optional()
address?: Address;
}
joiful has extensive JSDoc / TSDoc comments.
You can browse the generated API docs online.
The joiful API is designed to closely match the joi API. One exception is validating the length of a string
, array
, etc, which is performed using .exactLength(n)
rather than .length(n)
. If you're familiar with the joi API, you should find joiful very easy to pickup.
If there's something you're not sure of you can see how it's done by looking at the unit tests. There is 100% coverage so most likely you'll find your scenario there. Otherwise feel free to open an issue.
Got an issue or a feature request? Log it.
Pull-requests are also very welcome.