simp-validator
v1.3.0
Published
[](https://github.com/dncgmh/simp-validator/releases/) [;The validate function takes a value and a rule, and returns a ValidationResult object. The ValidationResult object contains a success property indicating whether the validation was successful, and a message property containing an error message if the validation failed.
- Validate an entire data object against a schema using the
schemaValidatefunction:
const data = {
name: 'John',
age: 25,
email: '[email protected]',
hobbies: ['reading', 'swimming'],
};
const schema: Schema = {
name: { type: 'string' },
age: { type: 'number', min: 18 },
email: { type: 'string', pattern: '^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$' },
hobbies: {
type: 'array',
items: { type: 'string', pattern: '^[a-zA-Z]+$' },
min: 1,
max: 5,
},
};
const validationResult = schemaValidate(data, schema);The schemaValidate function takes a data object and a schema, and returns a SchemaValidationResult object. The SchemaValidationResult object contains a success property indicating whether the validation was successful, a data property containing the parsed data if the validation was successful, and a details property containing an object with error messages for each field if the validation failed.
In this example, we're validating an object with several fields. The name field is a required string. The age field is a number that must be at least 18. The email field is a string that must match a specific pattern. The hobbies field is an array of strings that must match a specific pattern, must have at least one item, must have no more than five items, and must have unique items
- Check the validation result:
if (validationResult.success) {
// Validation succeeded
console.log('Validation passed!');
} else {
// Validation failed
console.error('Validation failed!');
// Value validation error message
console.error('Error messages:', validationResult.message);
// Schema validation error object
console.error('Error messages:', validationResult.details);
}Check the success property of the ValidationResult or SchemaValidationResult object to determine whether the validation was successful. If the validation failed, check the message or details property for error messages.
- Generate a schema from a TypeScript interface using the
toSchemafunction:
const rules: Rule[] = [
{ name: 'username', type: 'string' },
{ name: 'age', type: 'number', optional: true },
{ name: 'email', type: 'string', pattern: '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}$' },
{ name: 'hobbies', type: 'array', items: { type: 'string', pattern: '^[a-zA-Z]+$' } },
];
const { schema, message } = toSchema(rules);
if (schema) {
console.log('Schema:', schema);
} else {
console.error('Error:', message);
}This code creates an array of Rule objects, calls the toSchema function with that array, and then logs the resulting schema object to the console if there were no errors. If there was an error, it logs the error message to the console instead.
Supported Data Types
The Simp Validator supports the following data types for validation:
- String
- Number
- Boolean
- Array
- Date
Each data type has its own set of validation rules that can be customized according to your needs.
API
validate(value: any, rule: Rule): ValidationResult
The validate function is used to validate a value against a given rule.
value: The value to validate.rule: The rule to validate against.
Returns a ValidationResult object containing the result of the validation.
schemaValidate(data: any, schema: Schema): SchemaValidationResult
The schemaValidate function is used to validate an entire data object against a schema.
data: The data object to validate.schema: The schema to validate against.
Returns a SchemaValidationResult object containing the result of the schema validation.
Rule
The Rule type represents a rule for validating a value. It has the following properties:
type: The allowed data type of the value.optional: A boolean indicating whether the value is optional. Defaults tofalse.valid: An array of valid values.- Additional properties depending on the data type:
- For
string:min,max,pattern,len - For
number:integer,min,max - For
boolean: No additional properties - For
array:items,min,max,len - For
date:min,max
- For
name(required for schema conversion): The name of the field.description: The description of the field.message: The custom error message to display if the validation fails.
Schema
The Schema type represents a schema for validating an entire data object. It is a record where the keys are the field names, and the values are the validation rules.
ValidationResult
Validate a value against a rule. Returns a ValidationResult object containing the result of the validation.
success: A boolean indicating whether the validation was successful.message: An error message if the validation failed.
SchemaValidationResult
Validate an entire data object against a schema. Returns a SchemaValidationResult object containing the result of the schema validation.
success: A boolean indicating whether the validation was successful.data: The parsed data if the validation was successful.details: An object containing error messages for each field if the validation failed.
