# sfn-validator

> Simple Friendly Node.js Validator.

Latest version **0.1.6** (published 2018-12-07) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 25/100 (F)** — status: abandoned.

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.6 |
| Published | 2018-12-07 |
| First published | 2017-11-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=4.0 |
| Dependencies | 3 |
| Unpacked size | 151.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ayon Lee |
| Maintainers | hyurl |
| Keywords | sfn, validator, validate, form, data validate |

## Links

- npm: https://www.npmjs.com/package/sfn-validator
- Repository: https://github.com/hyurl/sfn-validator
- Issues: https://github.com/Hyurl/sfn-validator/issues
- npm.io page: https://npm.io/package/sfn-validator

## Dependencies (3)

- [isbn](https://npm.io/package/isbn.md) ^0.4.0
- [is-ip](https://npm.io/package/is-ip.md) ^2.0.0
- [is-color](https://npm.io/package/is-color.md) ^1.0.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.6 (latest) — 2018-12-07
- 0.1.5 — 2018-09-06
- 0.1.4 — 2018-05-12
- 0.1.3 — 2018-05-11
- 0.1.2 — 2018-05-10
- 0.1.1 — 2018-02-04
- 0.1.0 — 2017-11-11
- 0.0.1 — 2017-11-11

## README

# SFN-Validator

**Simple Friendly Node.js Validator.**

## Install

```sh
npm install sfn-validator --save
```

## Environment

This package supports any version of Node.js that higher than 4.0.0.

## Example

```javascript
const Validator = require("sfn-validator");

var validator = new Validator({
    name: {
        type: "string",
        required: true,
        length: [3, 18]
    },
    email: {
        type: "email",
        required: true,
        strict: true,
    },
    password: {
        type: "string",
        required: true,
        length: [8, 18],
        msg: {
            required: "You must provide a 'password' in this form.",
            equals: "The length of the 'password' must between 8 and 18.",
        }
    },
    check_password: {
        type: "string",
        required: true,
        equals: "password",
    },
    url: "url" // Short-hand, equal to {type: "url"}.
});

try{
    validator.validate(data);
}catch(e){
    console.log(e);
}
```

## API

- `new Validator(rules: any)` Creates a validator with specified rules.
- `validator.validate(data: any)` Checks if the input data are all 
    valid.
    - alias: `validator.check()`
- `validator.filter(data: any): any` Filters input data according to 
    the rules.

## Rules

Every field of rule can be defined with these properties:

- `type` The data type to check, could be:
    - `string`
    - `number`
    - `boolean`
    - `object` Carries children fields.
    - `array`
    - `email`
    - `url`
    - `date` Date instance or a valid date string.
    - `time` Unix timestamp or time string.
    - `color` Color name, hex, RGB, RGBA color string.
    - `ipv4`
    - `ipv6`
    - `mac` Media Access Control address (Machine address).
    - `uuid` Universal Unique Identifier. 
    - `isbn` International Standard Book Number.
    - `ascii` Only contains ASCII characters.
    - `base64` Only contains base64 characters.
    - `json`
    - `data-uri`
- `required` Whether the field is required (`true`) or not (`false`, default).
- `equals` The value of this field should be equal to the given field's.
- `msg` Customize error messages, could be a string that sets all messages,
    or an object sets messages for `type`, `required` and `equals`. If the 
    field has a `length` or `range` property, their error messages could be 
    set as well.
- `length` Only for `string`, `email`, `url`, `ascii`, `base64`, `json`, 
    `data-uri` and `array`, could be a number sets an exact length, or an 
    array sets the minimum and maximum length.
- `range` Only for `number`, an array set the range (minimum and maximum) of 
    the data value.
- `strict` Strict mode, only for `number`, `boolean`, `email`, `url`, `ipv4`, 
    `isbn`, it's `false` by default for most types except `number`, which is 
    `true` by default.
- `children` Only for `object`, to set children rules.

## More Details About Strict Mode

- `number` When non-strict, treat numeric string as number.
- `email` If strict, the email address can only contain ASCII characters, and 
    the hostname must not be `localhost`.
- `url` If strict, the hostname can only contain ASCII characters, and the 
    hostname must not be `localhost`.
- `ipv4` If strict, reject private and reserved IP addresses.
- `isbn` If strict, the book number must be prettified with hyphenates (`-`).

## Pre-check of Rule Definition

When you call `new Validator()` and pass the rules, the program will perform 
checking on the rules you provided, if any of them is invalid, an error will 
be thrown and tells you where is incorrect in your rules, so that you can find 
and fix it as soon as possible.

## What Will Happen If Validating Failed?

If validating failed, `validator.validate()` will thrown an error message.
You can catch the error with a `try...catch...` block. This package will 
modify the initial error stack and only shows you the useful parts, so it's 
very friendly on user-experience.

## About Children Rule

If you specify a field's type `object`, then this field can and must carry 
children fields, like this example:

```javascript
var rules = {
    multi: {
        type: "object",
        children: {
            name: "string",
            email: "email"
        }
    }
};
// And input data should like this:
var data = {
    multi: {
        name: "John",
        email: "john@example.com"
    }
};
```

Children rules can be nested and unlimited, **BUT**, if you use `equals` 
property, be aware that it can only refers to the same depth of the rule.

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