flex-validator
v1.0.0
Published
A flexible schema-based validation library for JavaScript and TypeScript
Downloads
5
Maintainers
Readme
Flex Validator
A flexible schema-based validation library for JavaScript and TypeScript.
Features
- Schema-based validation: Define validation rules using a simple and intuitive schema object.
- Type-safe: Written in TypeScript for robust type checking and autocompletion.
- Flexible: Supports validation of nested objects, arrays, and primitive types.
- Extensible: Easily extendable with custom validation logic.
- Comprehensive validation options:
- Required fields
- String patterns (regex)
- Enums
- Number ranges (min/max values)
- Array length constraints (min/max length)
Installation
npm install flex-validatorUsage
Basic Usage
To use Flex Validator, you need to define a validation schema and then use the isValid function to validate your data against that schema.
import { isValid, ObjectValidationScheme } from 'flex-validator';
const schema: ObjectValidationScheme = {
name: { type: 'string', required: true },
age: { type: 'number', required: true, minValue: 18 },
email: { type: 'string', required: true, regex: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ },
};
const data = {
name: 'John Doe',
age: 30,
email: '[email protected]',
};
if (isValid(data, schema)) {
console.log('Validation successful!');
} else {
console.log('Validation failed!');
}Nested Objects
You can validate nested objects by defining an objectScheme within your schema.
import { isValid, ObjectValidationScheme } from 'flex-validator';
const schema: ObjectValidationScheme = {
user: {
type: 'object',
required: true,
objectScheme: {
firstName: { type: 'string', required: true },
lastName: { type: 'string', required: true },
},
},
address: {
type: 'object',
required: true,
objectScheme: {
street: { type: 'string', required: true },
city: { type: 'string', required: true },
zipCode: { type: 'string', required: true, regex: /^\d{5}$/ },
},
},
};
const data = {
user: {
firstName: 'John',
lastName: 'Doe',
},
address: {
street: '123 Main St',
city: 'Anytown',
zipCode: '12345',
},
};
if (isValid(data, schema)) {
console.log('Validation successful!');
} else {
console.log('Validation failed!');
}Arrays
You can validate arrays by setting the isArray property to true.
import { isValid, ObjectValidationScheme } from 'flex-validator';
const schema: ObjectValidationScheme = {
tags: {
type: 'string',
isArray: true,
minLength: 1,
maxLength: 5,
},
users: {
type: 'object',
isArray: true,
objectScheme: {
name: { type: 'string', required: true },
email: { type: 'string', required: true, regex: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ },
},
},
};
const data = {
tags: ['typescript', 'javascript', 'validation'],
users: [
{ name: 'John Doe', email: '[email protected]' },
{ name: 'Jane Doe', email: '[email protected]' },
],
};
if (isValid(data, schema)) {
console.log('Validation successful!');
} else {
console.log('Validation failed!');
}Enums
You can validate that a string value is one of a predefined set of values using the enum property.
import { isValid, ObjectValidationScheme } from 'flex-validator';
const schema: ObjectValidationScheme = {
status: {
type: 'string',
required: true,
enum: ['active', 'inactive', 'pending'],
},
};
const data = {
status: 'active',
};
if (isValid(data, schema)) {
console.log('Validation successful!');
} else {
console.log('Validation failed!');
}