# simp-validator

> [![GitHub release](https://img.shields.io/github/release/dncgmh/simp-validator?include_prereleases=&sort=semver&color=blue)](https://github.com/dncgmh/simp-validator/releases/) [![issues - simp-validator](https://img.shields.io/github/issues/dncgmh/simp-v

Latest version **1.3.0** (published 2023-06-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install simp-validator
pnpm add simp-validator
yarn add simp-validator
bun add simp-validator
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2023-06-01 |
| First published | 2023-05-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 28.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | dncgmh |
| Maintainers | dncgmh |
| Keywords | validator, validate, validation |

## Links

- npm: https://www.npmjs.com/package/simp-validator
- Repository: https://github.com/dncgmh/simp-validator
- Homepage: https://github.com/dncgmh/simp-validator#readme
- Issues: https://github.com/dncgmh/simp-validator/issues
- npm.io page: https://npm.io/package/simp-validator

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.3.0 (latest) — 2023-06-01
- 1.2.2 — 2023-05-26
- 1.2.1 — 2023-05-26
- 1.2.0 — 2023-05-26
- 1.1.3 — 2023-05-08
- 1.1.2 — 2023-05-07
- 1.1.1 — 2023-05-07
- 1.1.0 — 2023-05-07
- 1.0.8 — 2023-05-05
- 1.0.7 — 2023-05-05
- 1.0.6 — 2023-05-05
- 1.0.5 — 2023-05-05
- 1.0.4 — 2023-05-05
- 1.0.3 — 2023-05-05
- 1.0.2 — 2023-05-05
- … 2 more at https://npm.io/package/simp-validator/versions

## README

# Simp Validator

[![GitHub release](https://img.shields.io/github/release/dncgmh/simp-validator?include_prereleases=&sort=semver&color=blue)](https://github.com/dncgmh/simp-validator/releases/)
[![issues - simp-validator](https://img.shields.io/github/issues/dncgmh/simp-validator)](https://github.com/dncgmh/simp-validator/issues)
[![CircleCI](https://img.shields.io/circleci/build/github/dncgmh/simp-validator/main)](https://circleci.com/gh/dncgmh/simp-validator)
[![codecov](https://codecov.io/gh/dncgmh/simp-validator/branch/main/graph/badge.svg)](https://codecov.io/gh/dncgmh/simp-validator)
[![npm](https://img.shields.io/npm/dw/simp-validator)](https://www.npmjs.com/package/simp-validator)
[![License](https://img.shields.io/badge/License-ISC-blue)](https://github.com/dncgmh/simp-validator/blob/main/LICENSE)

The Simp Validator is a lightweight JavaScript library for validating data against a schema. It's simple, flexible, and easily transferable over the network. It can be used in both browser and Node.js environments, making it versatile for various development scenarios. Its key feature is the ability to validate data on both the frontend and backend using a single schema, which can be easily migrated or serialized/deserialized without impacting the validation process.

## Installation

You can install the Simp Validator using npm:

```
npm install simp-validator
```

## Usage

To use the Simp Validator in your project, follow these steps:

1. Import the necessary functions and types from the library:

```typescript
import { validate, schemaValidate, Rule, Schema, toSchema } from 'simp-validator';
```

The `validate` function is used to validate a single value against a rule. The `schemaValidate` function is used to validate an entire data object against a schema. The `Rule` and `Schema` types are used to define validation rules and schemas, respectively. The `toSchema` function is used to generate a schema from a TypeScript interface.

2. Define your validation rules using the `Rule` type:

```typescript
const rule: Rule = {
  type: 'string',
  min: 2,
  max: 10,
  pattern: '^[a-zA-Z]+$',
  len: 5,
};
```

The `Rule` type is an object that defines the validation rules for a single value. The `type` property is required and specifies the data type of the value being validated. Other properties depend on the data type being validated. In this example, we're validating a string value that is required, must be between 2 and 10 characters long, must match the pattern /^[a-zA-Z]+$/, and must be exactly 5 characters long.

3. Validate a single value using the `validate` function:

```typescript
const value = 'hello';
const result = validate(value, rule);
```

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.

4. Validate an entire data object against a schema using the `schemaValidate` function:

```typescript
const data = {
  name: 'John',
  age: 25,
  email: 'john@example.com',
  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

5. Check the validation result:

```typescript
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.

6. Generate a schema from a TypeScript interface using the `toSchema` function:

```typescript
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 to `false`.
- `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`
- `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.

---
_Source: https://npm.io/package/simp-validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
