Top Related Projects
String validation
Dead simple Object schema validation
TypeScript-first schema validation with static type inference
The most powerful data validation library for JS
Decorator-based property validation for classes.
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Quick Overview
Vest is a lightweight JavaScript validation framework designed for complex forms and data structures. It offers a declarative approach to validation, allowing developers to define validation rules in a clear and organized manner. Vest supports asynchronous validations and provides a suite of built-in validation functions.
Pros
- Flexible and extensible, allowing custom validation rules
- Supports both synchronous and asynchronous validations
- Provides detailed validation results, including field-specific errors
- Lightweight and has no dependencies
Cons
- Learning curve for developers new to declarative validation approaches
- Limited built-in localization support
- May require additional setup for complex validation scenarios
- Documentation could be more comprehensive for advanced use cases
Code Examples
- Basic validation suite:
import { create, test, enforce } from 'vest';
const suite = create((data = {}) => {
test('username', 'Username is required', () => {
enforce(data.username).isNotEmpty();
});
test('email', 'Email must be valid', () => {
enforce(data.email).isEmail();
});
});
- Asynchronous validation:
import { create, test, enforce } from 'vest';
const suite = create((data = {}) => {
test('username', 'Username must be unique', async () => {
const isUnique = await checkUsernameUniqueness(data.username);
enforce(isUnique).isTruthy();
});
});
- Custom validation function:
import { create, test, enforce } from 'vest';
enforce.extend({ isValidPostalCode: (value) => /^\d{5}(-\d{4})?$/.test(value) });
const suite = create((data = {}) => {
test('postalCode', 'Invalid postal code', () => {
enforce(data.postalCode).isValidPostalCode();
});
});
Getting Started
To use Vest in your project, first install it via npm:
npm install vest
Then, create a validation suite:
import { create, test, enforce } from 'vest';
const validationSuite = create((data = {}) => {
test('name', 'Name is required', () => {
enforce(data.name).isNotEmpty();
});
test('email', 'Email must be valid', () => {
enforce(data.email).isEmail();
});
});
// Run the validation
const result = validationSuite({ name: 'John', email: 'john@example.com' });
console.log(result.hasErrors()); // false
Competitor Comparisons
String validation
Pros of validator.js
- Extensive library of built-in validation rules
- Supports string validation for URLs, emails, and more
- Well-established project with a large community
Cons of validator.js
- Limited to string validation only
- Lacks support for complex, nested object validation
- No built-in state management for form validation
Code Comparison
validator.js:
import validator from 'validator';
const isValid = validator.isEmail('test@example.com');
console.log(isValid); // true
Vest:
import { create, test, enforce } from 'vest';
const suite = create((data = {}) => {
test('email', 'Email is invalid', () => {
enforce(data.email).isEmail();
});
});
const result = suite({ email: 'test@example.com' });
console.log(result.hasErrors()); // false
Vest offers a more comprehensive approach to form validation, allowing for complex validation logic and state management. validator.js, on the other hand, provides a simpler API focused on string validation. While validator.js excels in its extensive library of string validation rules, Vest's flexibility and support for object validation make it more suitable for complex form validation scenarios.
Dead simple Object schema validation
Pros of Yup
- More mature and widely adopted in the React ecosystem
- Supports asynchronous validation out of the box
- Offers a wider range of built-in validation methods
Cons of Yup
- Larger bundle size, which may impact performance in larger applications
- Less flexible for complex, nested object validations
- Steeper learning curve for advanced use cases
Code Comparison
Yup:
const schema = yup.object().shape({
name: yup.string().required(),
age: yup.number().positive().integer().required(),
});
schema.validate({ name: 'John', age: 30 })
.then(valid => console.log(valid))
.catch(error => console.log(error));
Vest:
const suite = vest.create('user_form', (data = {}) => {
test('name', 'Name is required', () => {
enforce(data.name).isNotEmpty();
});
test('age', 'Age must be a positive integer', () => {
enforce(data.age).isNumber().positive().integer();
});
});
const result = suite({ name: 'John', age: 30 });
console.log(result.hasErrors());
Both libraries offer robust validation capabilities, but Yup provides a more declarative approach with its schema definition, while Vest offers a more imperative style with individual test functions. Vest's approach may be more flexible for complex scenarios, while Yup's schema-based validation can be more concise for simpler use cases.
TypeScript-first schema validation with static type inference
Pros of Zod
- More comprehensive type inference and static type checking
- Broader ecosystem with plugins and integrations
- Better support for complex object schemas and nested structures
Cons of Zod
- Larger bundle size and potentially slower performance
- Steeper learning curve for complex use cases
- Less focus on form validation specific features
Code Comparison
Zod schema definition:
const UserSchema = z.object({
username: z.string().min(3).max(20),
email: z.string().email(),
age: z.number().min(18).optional(),
});
Vest validation suite:
suite('user', (data) => {
test('username', 'Username is required', () => {
enforce(data.username).isNotEmpty().longerThan(2).shorterThan(21);
});
test('email', 'Invalid email', () => {
enforce(data.email).isEmail();
});
test('age', 'Must be 18 or older', () => {
enforce(data.age).optional().greaterThanOrEquals(18);
});
});
Both libraries offer schema validation, but Zod focuses on TypeScript integration and type inference, while Vest provides a more declarative approach tailored for form validation scenarios.
The most powerful data validation library for JS
Pros of Joi
- More mature and widely adopted, with a larger ecosystem and community support
- Offers a broader range of validation features and data types
- Provides built-in coercion and type conversion capabilities
Cons of Joi
- Heavier and more complex, potentially impacting performance for simpler use cases
- Steeper learning curve due to its extensive API and configuration options
- Less focused on client-side validation compared to Vest
Code Comparison
Joi:
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
email: Joi.string().email().required()
});
const { error, value } = schema.validate({ username: 'john_doe', email: 'john@example.com' });
Vest:
import { create, test, enforce } from 'vest';
const suite = create((data = {}) => {
test('username', 'Username is required', () => {
enforce(data.username).isNotEmpty().longerThan(2).shorterThan(31);
});
test('email', 'Email is invalid', () => {
enforce(data.email).isNotEmpty().isEmail();
});
});
const result = suite({ username: 'john_doe', email: 'john@example.com' });
Both libraries offer robust validation capabilities, but Joi focuses on server-side validation with a more extensive feature set, while Vest is designed for both client and server-side validation with a simpler API and better performance for smaller applications.
Decorator-based property validation for classes.
Pros of class-validator
- Integrates seamlessly with TypeScript and decorators, providing type-safe validation
- Offers a wide range of built-in validation decorators for common use cases
- Supports custom validation functions and asynchronous validation
Cons of class-validator
- Requires the use of classes and decorators, which may not fit all project structures
- Can be more verbose for simple validation scenarios
- Limited flexibility in defining custom validation rules compared to Vest
Code Comparison
class-validator:
import { IsString, MinLength, IsEmail } from 'class-validator';
class User {
@IsString()
@MinLength(2)
name: string;
@IsEmail()
email: string;
}
Vest:
import { create, test, enforce } from 'vest';
const suite = create((data = {}) => {
test('name', 'Name is required', () => {
enforce(data.name).isNotEmpty().longerThan(1);
});
test('email', 'Email must be valid', () => {
enforce(data.email).isEmail();
});
});
Both libraries offer robust validation capabilities, but class-validator is more tightly coupled with TypeScript and object-oriented programming paradigms, while Vest provides a more flexible, function-based approach to validation that can be easily integrated into various project structures.
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Pros of Ajv
- Highly performant JSON schema validator with extensive support for JSON Schema standards
- Supports custom keywords and formats for advanced validation scenarios
- Large ecosystem with plugins and integrations for various frameworks
Cons of Ajv
- Steeper learning curve due to its comprehensive feature set
- Primarily focused on JSON schema validation, which may be overkill for simpler use cases
- Configuration can be more complex for basic validation needs
Code Comparison
Ajv:
const Ajv = require("ajv")
const ajv = new Ajv()
const schema = {
type: "object",
properties: {
foo: { type: "integer" },
bar: { type: "string" }
},
required: ["foo"],
additionalProperties: false
}
const validate = ajv.compile(schema)
Vest:
import { create, test, enforce } from 'vest';
const suite = create((data = {}) => {
test('foo', 'Foo is required', () => {
enforce(data.foo).isNotEmpty();
});
test('bar', 'Bar must be a string', () => {
enforce(data.bar).isString();
});
});
The code comparison shows that Ajv uses a JSON schema approach for defining validation rules, while Vest employs a more programmatic and declarative style. Ajv's schema is more concise for complex structures, but Vest's approach may be more intuitive for developers familiar with unit testing patterns.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Vest - Declarative validations framework
Vest is a declarative validations framework designed to simplify the process of writing and maintaining form validations for your web application. Inspired by popular unit testing libraries such as Mocha and Jest, Vest allows developers to describe their validation requirements using a suite-like syntax, separating validation logic from feature logic to create more maintainable and readable code.
Vest's framework-agnostic approach means that it can be used with any UI framework, or without any framework at all. With Vest, you can reduce code bloat, improve feature readability and maintainability, and enhance the user experience of your web application.
test('username', 'Username is required', () => {
enforce(data.username).isNotBlank();
});
test('username', 'Username must be at least 3 chars', () => {
enforce(data.username).longerThanOrEquals(3);
});
test('username', 'Username already taken', async () => {
await doesUserExist(data.username);
});
Installation
npm i vest
Motivation
Building web applications often involves writing complex forms that require validation. As the complexity of these forms increases, so does the complexity of the validation logic required to ensure data is accurate and complete.
At this point, developers may start to experience issues with code bloat, poor maintainability, and difficulty in managing validation logic across different features of an application. This can lead to bugs, errors, and a poor user experience.
Vest was designed to address these issues by providing a simple, intuitive way to write form validation that is easy to learn, scalable, and extensible. By separating validation logic from feature logic, Vest helps developers create maintainable code that is easy to update, debug, and refactor.
With Vest, developers can reduce the complexity and increase the readability of their code, leading to more efficient development cycles, fewer bugs, and a better user experience overall.
Why Vest?
Writing form validations can be time-consuming and complex, especially as your web application grows and evolves over time. Vest simplifies the process by providing a set of powerful tools that take care of the annoying parts for you, such as managing validation state and handling async validations.
Vest's declarative syntax is also designed to be easy to learn, especially for developers who are already familiar with unit testing frameworks. With Vest, you can leverage your existing knowledge to write effective form validations quickly and easily.
ð¡ Easy to Learn
Vest adopts the syntax and style of unit testing frameworks, so you can leverage the knowledge you already have to write your form validations.
ð¨ Framework Agnostic
Vest is framework-agnostic, which means you can use it with any UI framework out there.
ð§ Takes Care of the Annoying Parts
Vest manages its validation state, handles async validations, and much more, so you don't have to.
𧩠Extendable
You can easily add new kinds of validations to Vest according to your needs.
â»ï¸ Reusable Validation Logic
Validation logic in Vest can be shared across multiple features in your app, making it easy to maintain and refactor your codebase.
𧬠Supports Declarative Syntax
Vest's declarative syntax makes it easy to describe your form or feature structure and write clear, concise validations.
𧪠Promotes Testing and Debugging
By separating validation logic from feature logic, Vest makes it easier to test and debug your code, which can save you time and reduce errors.
Getting Started
Here are some code sandboxes to get you started:
- React
- Vue
- Svelte
- Vanilla
- Angular
- with an accompanying walk-through and course by (by @brechtbilliet)
Contribute
Information describing how to contribute can be found here:
Top Related Projects
String validation
Dead simple Object schema validation
TypeScript-first schema validation with static type inference
The most powerful data validation library for JS
Decorator-based property validation for classes.
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot