Full Code of yiminghe/async-validator for AI

master b9a84a7313e8 cached
58 files
114.6 KB
30.5k tokens
122 symbols
1 requests
Download .txt
Repository: yiminghe/async-validator
Branch: master
Commit: b9a84a7313e8
Files: 58
Total size: 114.6 KB

Directory structure:
gitextract__9gnz8i2/

├── .babelrc.js
├── .editorconfig
├── .gitignore
├── .np-config.json
├── .prettierrc
├── .travis.yml
├── .yarnrc.yml
├── HISTORY.md
├── LICENSE.md
├── README.md
├── __tests__/
│   ├── any.spec.ts
│   ├── array.spec.ts
│   ├── date.spec.ts
│   ├── deep.spec.ts
│   ├── enum.spec.ts
│   ├── messages.spec.ts
│   ├── number.spec.ts
│   ├── object.spec.ts
│   ├── pattern.spec.ts
│   ├── promise.spec.ts
│   ├── required.spec.ts
│   ├── string.spec.ts
│   ├── unicode.spec.ts
│   ├── url.spec.ts
│   └── validator.spec.ts
├── package.json
├── scripts/
│   ├── runTest.ts
│   └── test.sh
├── src/
│   ├── index.ts
│   ├── interface.ts
│   ├── messages.ts
│   ├── rule/
│   │   ├── enum.ts
│   │   ├── index.ts
│   │   ├── pattern.ts
│   │   ├── range.ts
│   │   ├── required.ts
│   │   ├── type.ts
│   │   ├── url.ts
│   │   └── whitespace.ts
│   ├── util.ts
│   └── validator/
│       ├── any.ts
│       ├── array.ts
│       ├── boolean.ts
│       ├── date.ts
│       ├── enum.ts
│       ├── float.ts
│       ├── index.ts
│       ├── integer.ts
│       ├── method.ts
│       ├── number.ts
│       ├── object.ts
│       ├── pattern.ts
│       ├── regexp.ts
│       ├── required.ts
│       ├── string.ts
│       └── type.ts
├── tests/
│   └── url.ts
└── tsconfig.json

================================================
FILE CONTENTS
================================================

================================================
FILE: .babelrc.js
================================================
console.log('Load babel config');

module.exports = api => {
  return {
    presets: [
      [
        '@babel/preset-env',
        api.env('test')
          ? { targets: { node: true } }
          : {
              loose: true,
              modules: false,
            },
      ],
      '@babel/preset-typescript',
    ],
  };
};


================================================
FILE: .editorconfig
================================================
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*.{js,css}]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2


================================================
FILE: .gitignore
================================================
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
node_modules
/.yarn/*
!/.yarn/patches
!/.yarn/plugins
!/.yarn/releases
!/.yarn/sdks
!/.yarn/versions

# Swap the comments on the following lines if you don't wish to use zero-installs
# Documentation here: https://yarnpkg.com/features/zero-installs
#!/.yarn/cache
/.pnp.*


# testing
/coverage

# production
build
pkg
dist

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

*.log
*.aes
cypress/screenshots
cypress/videos
cypress/logs
cypress/fixtures/profile.json
cypress/fixtures/users.json
.history

================================================
FILE: .np-config.json
================================================
{
    "yarn": false
}

================================================
FILE: .prettierrc
================================================
{
  "singleQuote": true,
  "trailingComma": "all"
}

================================================
FILE: .travis.yml
================================================
language: node_js

notifications:
    email:
        - yiminghe@gmail.com

node_js:
  - 16.13.1

before_install:
  - corepack enable

script:
  - yarn run coverage


================================================
FILE: .yarnrc.yml
================================================
nodeLinker: node-modules

================================================
FILE: HISTORY.md
================================================
# History
----

## 4.0.0 / 2021-08-11

- full ts support
- support return transformed value when pass validation(promise and callback): https://github.com/yiminghe/async-validator/pull/277

## 3.5.0 / 2020-11-12

- https://github.com/yiminghe/async-validator/pull/256/files

## 3.4.0 / 2020-08-05

- https://github.com/yiminghe/async-validator/pull/247
- https://github.com/yiminghe/async-validator/pull/246
- https://github.com/yiminghe/async-validator/pull/245
- https://github.com/yiminghe/async-validator/pull/240

## 3.3.0 / 2020-05-07

- expose validators: https://github.com/yiminghe/async-validator/pull/232

## 3.2.0 / 2019-10-16

- support `any` type: https://github.com/yiminghe/async-validator/pull/190

## 3.1.0 / 2019-09-09

- add d.ts

## 3.0.0 / 2019-08-07

- Enum validates `false` value: https://github.com/yiminghe/async-validator/pull/164

## 2.0.0 / 2019-07-26

- use @pika/pack

## 1.11.3 / 2019-06-28

- support suppressWarning option when validate

## 1.11.1 / 2019-04-22

- support message as function

## 1.11.0 / 2019-03-22

- support promise usage(asyncValidator)

## 1.10.1 / 2018-12-18

- support override warning

## 1.10.0 / 2018-10-17

- revert promise

## 1.9.0 / 2018-10-10

- .validate returns promise

## 1.8.0 / 2017-08-16

- validator support return promise.

## 1.7.0 / 2017-06/09

- add es
- support string patter

## 1.6.0 / 2016-03-30

- support defaultField

## 1.5.0 / 2016-02-02

- support deep merge with default messages
- support rule message of any type(exp: jsx)

## 1.4.0 / 2015-01-12

- fix first option. 
- add firstFields option.
- see tests/validator.spec.js


================================================
FILE: LICENSE.md
================================================
The MIT License (MIT)

Copyright (c) 2014-present yiminghe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


================================================
FILE: README.md
================================================
# async-validator

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]
[![npm bundle size (minified + gzip)][bundlesize-image]][bundlesize-url]

[npm-image]: https://img.shields.io/npm/v/async-validator.svg?style=flat-square
[npm-url]: https://npmjs.org/package/async-validator
[travis-image]:https://app.travis-ci.com/yiminghe/async-validator.svg?branch=master
[travis-url]: https://app.travis-ci.com/github/yiminghe/async-validator
[coveralls-image]: https://img.shields.io/coveralls/yiminghe/async-validator.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/yiminghe/async-validator?branch=master
[node-image]: https://img.shields.io/badge/node.js-%3E=4.0.0-green.svg?style=flat-square
[node-url]: https://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/async-validator.svg?style=flat-square
[download-url]: https://npmjs.org/package/async-validator
[bundlesize-image]: https://img.shields.io/bundlephobia/minzip/async-validator.svg?label=gzip%20size
[bundlesize-url]: https://bundlephobia.com/result?p=async-validator

Validate form asynchronous. A variation of https://github.com/freeformsystems/async-validate

## Install

```bash
npm i async-validator
```

## Usage

Basic usage involves defining a descriptor, assigning it to a schema and passing the object to be validated and a callback function to the `validate` method of the schema:

```js
import Schema from 'async-validator';
const descriptor = {
  name: {
    type: 'string',
    required: true,
    validator: (rule, value) => value === 'muji',
  },
  age: {
    type: 'number',
    asyncValidator: (rule, value) => {
      return new Promise((resolve, reject) => {
        if (value < 18) {
          reject('too young');  // reject with error message
        } else {
          resolve();
        }
      });
    },
  },
};
const validator = new Schema(descriptor);
validator.validate({ name: 'muji' }, (errors, fields) => {
  if (errors) {
    // validation failed, errors is an array of all errors
    // fields is an object keyed by field name with an array of
    // errors per field
    return handleErrors(errors, fields);
  }
  // validation passed
});

// PROMISE USAGE
validator.validate({ name: 'muji', age: 16 }).then(() => {
  // validation passed or without error message
}).catch(({ errors, fields }) => {
  return handleErrors(errors, fields);
});
```

## API

### Validate

```js
function(source, [options], callback): Promise
```

* `source`: The object to validate (required).
* `options`: An object describing processing options for the validation (optional).
* `callback`: A callback function to invoke when validation completes (optional).

The method will return a Promise object like:
* `then()`,validation passed
* `catch({ errors, fields })`,validation failed, errors is an array of all errors, fields is an object keyed by field name with an array of errors per field

### Options

* `suppressWarning`: Boolean, whether to suppress internal warning about invalid value.

* `first`: Boolean, Invoke `callback` when the first validation rule generates an error,
no more validation rules are processed.
If your validation involves multiple asynchronous calls (for example, database queries) and you only need the first error use this option.

* `firstFields`: Boolean|String[], Invoke `callback` when the first validation rule of the specified field generates an error,
no more validation rules of the same field are processed.  `true` means all fields.

### Rules

Rules may be functions that perform validation.

```js
function(rule, value, callback, source, options)
```

* `rule`: The validation rule in the source descriptor that corresponds to the field name being validated. It is always assigned a `field` property with the name of the field being validated.
* `value`: The value of the source object property being validated.
* `callback`: A callback function to invoke once validation is complete. It expects to be passed an array of `Error` instances to indicate validation failure. If the check is synchronous, you can directly return a ` false ` or ` Error ` or ` Error Array `.
* `source`: The source object that was passed to the `validate` method.
* `options`: Additional options.
* `options.messages`: The object containing validation error messages, will be deep merged with defaultMessages.

The options passed to `validate` or `asyncValidate` are passed on to the validation functions so that you may reference transient data (such as model references) in validation functions. However, some option names are reserved; if you use these properties of the options object they are overwritten. The reserved properties are `messages`, `exception` and `error`.

```js
import Schema from 'async-validator';
const descriptor = {
  name(rule, value, callback, source, options) {
    const errors = [];
    if (!/^[a-z0-9]+$/.test(value)) {
      errors.push(new Error(
        util.format('%s must be lowercase alphanumeric characters', rule.field),
      ));
    }
    return errors;
  },
};
const validator = new Schema(descriptor);
validator.validate({ name: 'Firstname' }, (errors, fields) => {
  if (errors) {
    return handleErrors(errors, fields);
  }
  // validation passed
});
```

It is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example:

```js
const descriptor = {
  email: [
    { type: 'string', required: true, pattern: Schema.pattern.email },
    { 
      validator(rule, value, callback, source, options) {
        const errors = [];
        // test if email address already exists in a database
        // and add a validation error to the errors array if it does
        return errors;
      },
    },
  ],
};
```

#### Type

Indicates the `type` of validator to use. Recognised type values are:

* `string`: Must be of type `string`. `This is the default type.`
* `number`: Must be of type `number`.
* `boolean`: Must be of type `boolean`.
* `method`: Must be of type `function`.
* `regexp`: Must be an instance of `RegExp` or a string that does not generate an exception when creating a new `RegExp`.
* `integer`: Must be of type `number` and an integer.
* `float`: Must be of type `number` and a floating point number.
* `array`: Must be an array as determined by `Array.isArray`.
* `object`: Must be of type `object` and not `Array.isArray`.
* `enum`: Value must exist in the `enum`.
* `date`: Value must be valid as determined by `Date`
* `url`: Must be of type `url`.
* `hex`: Must be of type `hex`.
* `email`: Must be of type `email`.
* `any`: Can be any type.

#### Required

The `required` rule property indicates that the field must exist on the source object being validated.

#### Pattern

The `pattern` rule property indicates a regular expression that the value must match to pass validation.

#### Range

A range is defined using the `min` and `max` properties. For `string` and `array` types comparison is performed against the `length`, for `number` types the number must not be less than `min` nor greater than `max`.

#### Length

To validate an exact length of a field specify the `len` property. For `string` and `array` types comparison is performed on the `length` property, for the `number` type this property indicates an exact match for the `number`, ie, it may only be strictly equal to `len`.

If the `len` property is combined with the `min` and `max` range properties, `len` takes precedence.

#### Enumerable

> Since version 3.0.0 if you want to validate the values `0` or `false` inside `enum` types, you have to include them explicitly.

To validate a value from a list of possible values use the `enum` type with a `enum` property listing the valid values for the field, for example:

```js
const descriptor = {
  role: { type: 'enum', enum: ['admin', 'user', 'guest'] },
};
```

#### Whitespace

It is typical to treat required fields that only contain whitespace as errors. To add an additional test for a string that consists solely of whitespace add a `whitespace` property to a rule with a value of `true`. The rule must be a `string` type.

You may wish to sanitize user input instead of testing for whitespace, see [transform](#transform) for an example that would allow you to strip whitespace.


#### Deep Rules

If you need to validate deep object properties you may do so for validation rules that are of the `object` or `array` type by assigning nested rules to a `fields` property of the rule.

```js
const descriptor = {
  address: {
    type: 'object',
    required: true,
    fields: {
      street: { type: 'string', required: true },
      city: { type: 'string', required: true },
      zip: { type: 'string', required: true, len: 8, message: 'invalid zip' },
    },
  },
  name: { type: 'string', required: true },
};
const validator = new Schema(descriptor);
validator.validate({ address: {} }, (errors, fields) => {
  // errors for address.street, address.city, address.zip
});
```

Note that if you do not specify the `required` property on the parent rule it is perfectly valid for the field not to be declared on the source object and the deep validation rules will not be executed as there is nothing to validate against.

Deep rule validation creates a schema for the nested rules so you can also specify the `options` passed to the `schema.validate()` method.

```js
const descriptor = {
  address: {
    type: 'object',
    required: true,
    options: { first: true },
    fields: {
      street: { type: 'string', required: true },
      city: { type: 'string', required: true },
      zip: { type: 'string', required: true, len: 8, message: 'invalid zip' },
    },
  },
  name: { type: 'string', required: true },
};
const validator = new Schema(descriptor);

validator.validate({ address: {} })
  .catch(({ errors, fields }) => {
    // now only errors for street and name    
  });
```

The parent rule is also validated so if you have a set of rules such as:

```js
const descriptor = {
  roles: {
    type: 'array',
    required: true,
    len: 3,
    fields: {
      0: { type: 'string', required: true },
      1: { type: 'string', required: true },
      2: { type: 'string', required: true },
    },
  },
};
```

And supply a source object of `{ roles: ['admin', 'user'] }` then two errors will be created. One for the array length mismatch and one for the missing required array entry at index 2.

#### defaultField

The `defaultField` property can be used with the `array` or `object` type for validating all values of the container.
It may be an `object` or `array` containing validation rules. For example:

```js
const descriptor = {
  urls: {
    type: 'array',
    required: true,
    defaultField: { type: 'url' },
  },
};
```

Note that `defaultField` is expanded to `fields`, see [deep rules](#deep-rules).

#### Transform

Sometimes it is necessary to transform a value before validation, possibly to coerce the value or to sanitize it in some way. To do this add a `transform` function to the validation rule. The property is transformed prior to validation and returned as promise result or callback result when pass validation.

```js
import Schema from 'async-validator';
const descriptor = {
  name: {
    type: 'string',
    required: true,
    pattern: /^[a-z]+$/,
    transform(value) {
      return value.trim();
    },
  },
};
const validator = new Schema(descriptor);
const source = { name: ' user  ' };

validator.validate(source)
  .then((data) => assert.equal(data.name, 'user'));

validator.validate(source,(errors, data)=>{
  assert.equal(data.name, 'user'));
});
```

Without the `transform` function validation would fail due to the pattern not matching as the input contains leading and trailing whitespace, but by adding the transform function validation passes and the field value is sanitized at the same time.


#### Messages

Depending upon your application requirements, you may need i18n support or you may prefer different validation error messages.

The easiest way to achieve this is to assign a `message` to a rule:

```js
{ name: { type: 'string', required: true, message: 'Name is required' } }
```

Message can be any type, such as jsx format.

```js
{ name: { type: 'string', required: true, message: '<b>Name is required</b>' } }
```

Message can also be a function, e.g. if you use vue-i18n:
```js
{ name: { type: 'string', required: true, message: () => this.$t( 'name is required' ) } }
```

Potentially you may require the same schema validation rules for different languages, in which case duplicating the schema rules for each language does not make sense.

In this scenario you could just provide your own messages for the language and assign it to the schema:

```js
import Schema from 'async-validator';
const cn = {
  required: '%s 必填',
};
const descriptor = { name: { type: 'string', required: true } };
const validator = new Schema(descriptor);
// deep merge with defaultMessages
validator.messages(cn);
...
```

If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `options.messages` property within the validation function.

#### asyncValidator

You can customize the asynchronous validation function for the specified field:

```js
const fields = {
  asyncField: {
    asyncValidator(rule, value, callback) {
      ajax({
        url: 'xx',
        value: value,
      }).then(function(data) {
        callback();
      }, function(error) {
        callback(new Error(error));
      });
    },
  },

  promiseField: {
    asyncValidator(rule, value) {
      return ajax({
        url: 'xx',
        value: value,
      });
    },
  },
};
```

#### validator

You can custom validate function for specified field:

```js
const fields = {
  field: {
    validator(rule, value, callback) {
      return value === 'test';
    },
    message: 'Value is not equal to "test".',
  },

  field2: {
    validator(rule, value, callback) {
      return new Error(`${value} is not equal to 'test'.`);
    },
  },
 
  arrField: {
    validator(rule, value) {
      return [
        new Error('Message 1'),
        new Error('Message 2'),
      ];
    },
  },
};
```

## FAQ

### How to avoid global warning

```js
import Schema from 'async-validator';
Schema.warning = function(){};
```

or
```js
globalThis.ASYNC_VALIDATOR_NO_WARNING = 1;
```

### How to check if it is `true`

Use `enum` type passing `true` as option.

```js
{
  type: 'enum',
  enum: [true],
  message: '',
}
```

## Test Case

```bash
npm test
```

## Coverage

```bash
npm run coverage
```

Open coverage/ dir

## License

Everything is [MIT](https://en.wikipedia.org/wiki/MIT_License).


================================================
FILE: __tests__/any.spec.ts
================================================
import Schema from '../src';

const testNoErrorsFor = value => done => {
  new Schema({
    v: {
      type: 'any',
    },
  }).validate(
    {
      v: value,
    },
    errors => {
      expect(errors).toBe(null);
      done();
    },
  );
};

const testRequiredErrorFor = value => done => {
  new Schema({
    v: {
      required: true,
      type: 'string',
    },
  }).validate(
    {
      v: value,
    },
    errors => {
      expect(errors.length).toBe(1);
      expect(errors[0].message).toBe('v is required');
      done();
    },
  );
};

describe('any', () => {
  it('allows null', testNoErrorsFor(null));
  it('allows undefined', testNoErrorsFor(undefined));
  it('allows strings', testNoErrorsFor('foo'));
  it('allows numbers', testNoErrorsFor(1));
  it('allows booleans', testNoErrorsFor(false));
  it('allows arrays', testNoErrorsFor([]));
  it('allows objects', testNoErrorsFor({}));
  it('rejects undefined when required', testRequiredErrorFor(undefined));
  it('rejects null when required', testRequiredErrorFor(null));
});


================================================
FILE: __tests__/array.spec.ts
================================================
import Schema from '../src';

describe('array', () => {
  it('works for type', done => {
    new Schema({
      v: {
        type: 'array',
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is not an array');
        done();
      },
    );
  });

  it('works for type and required', done => {
    new Schema({
      v: {
        required: true,
        type: 'array',
      },
    }).validate(
      {
        v: '',
      },
      (errors, fields) => {
        expect(errors.length).toBe(1);
        expect(fields).toMatchInlineSnapshot(`
          Object {
            "v": Array [
              Object {
                "field": "v",
                "fieldValue": "",
                "message": "v is not an array",
              },
            ],
          }
        `);
        expect(errors[0].message).toBe('v is not an array');
        done();
      },
    );
  });

  it('works for none require', done => {
    new Schema({
      v: {
        type: 'array',
      },
    }).validate(
      {
        v: [],
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for empty array', done => {
    new Schema({
      v: {
        required: true,
        type: 'array',
      },
    }).validate(
      {
        v: [],
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for undefined array', done => {
    new Schema({
      v: {
        type: 'array',
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for undefined array and required', done => {
    new Schema({
      v: {
        required: true,
        type: 'array',
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for undefined array and defaultField', done => {
    new Schema({
      v: {
        type: 'array',
        defaultField: { type: 'string' },
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for null array', done => {
    new Schema({
      v: {
        required: true,
        type: 'array',
      },
    }).validate(
      {
        v: null,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for none empty', done => {
    new Schema({
      v: {
        required: true,
        type: 'array',
        message: 'haha',
      },
    }).validate(
      {
        v: [1],
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for empty array with min', done => {
    new Schema({
      v: {
        min: 1,
        max: 3,
        type: 'array',
      },
    }).validate(
      {
        v: [],
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v must be between 1 and 3 in length');
        done();
      },
    );
  });

  it('works for empty array with max', done => {
    new Schema({
      v: {
        min: 1,
        max: 3,
        type: 'array',
      },
    }).validate(
      {
        v: [1, 2, 3, 4],
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v must be between 1 and 3 in length');
        done();
      },
    );
  });
});


================================================
FILE: __tests__/date.spec.ts
================================================
import Schema from '../src';

describe('date', () => {
  it('required works for undefined', done => {
    new Schema({
      v: {
        type: 'date',
        required: true,
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('required works for ""', done => {
    new Schema({
      v: {
        type: 'date',
        required: true,
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('required works for non-date type', done => {
    new Schema({
      v: {
        type: 'date',
        required: true,
      },
    }).validate(
      {
        v: {},
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is not a date');
        done();
      },
    );
  });

  it('required works for "timestamp"', done => {
    new Schema({
      v: {
        type: 'date',
        required: true,
      },
    }).validate(
      {
        v: 1530374400000,
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });
});


================================================
FILE: __tests__/deep.spec.ts
================================================
import Schema, { Rules } from '../src';

describe('deep', () => {
  it('deep array specific validation', done => {
    new Schema({
      v: {
        required: true,
        type: 'array',
        fields: {
          '0': [{ type: 'string' }],
          '1': [{ type: 'string' }],
        },
      },
    }).validate(
      {
        v: [1, 'b'],
      },
      (errors, fields) => {
        expect(errors.length).toBe(1);
        expect(fields).toMatchInlineSnapshot(`
          Object {
            "v.0": Array [
              Object {
                "field": "v.0",
                "fieldValue": 1,
                "message": "v.0 is not a string",
              },
            ],
          }
        `);
        expect(errors[0].message).toBe('v.0 is not a string');
        done();
      },
    );
  });

  it('deep object specific validation', done => {
    new Schema({
      v: {
        required: true,
        type: 'object',
        fields: {
          a: [{ type: 'string' }],
          b: [{ type: 'string' }],
        },
      },
    }).validate(
      {
        v: {
          a: 1,
          b: 'c',
        },
      },
      (errors, fields) => {
        expect(errors.length).toBe(1);
        expect(fields).toMatchInlineSnapshot(`
          Object {
            "v.a": Array [
              Object {
                "field": "v.a",
                "fieldValue": 1,
                "message": "v.a is not a string",
              },
            ],
          }
        `);
        expect(errors[0].message).toBe('v.a is not a string');
        done();
      },
    );
  });

  describe('defaultField', () => {
    it('deep array all values validation', done => {
      new Schema({
        v: {
          required: true,
          type: 'array',
          defaultField: [{ type: 'string' }],
        },
      }).validate(
        {
          v: [1, 2, 'c'],
        },
        (errors, fields) => {
          expect(errors.length).toBe(2);
          expect(fields).toMatchInlineSnapshot(`
            Object {
              "v.0": Array [
                Object {
                  "field": "v.0",
                  "fieldValue": 1,
                  "message": "v.0 is not a string",
                },
              ],
              "v.1": Array [
                Object {
                  "field": "v.1",
                  "fieldValue": 2,
                  "message": "v.1 is not a string",
                },
              ],
            }
          `);
          expect(errors[0].message).toBe('v.0 is not a string');
          expect(errors[1].message).toBe('v.1 is not a string');
          done();
        },
      );
    });

    it('deep transform array all values validation', done => {
      new Schema({
        v: {
          required: true,
          type: 'array',
          defaultField: [{ type: 'number', max: 0, transform: Number }],
        },
      }).validate(
        {
          v: ['1', '2'],
        },
        (errors, fields) => {
          expect(errors.length).toBe(2);
          expect(fields).toMatchInlineSnapshot(`
            Object {
              "v.0": Array [
                Object {
                  "field": "v.0",
                  "fieldValue": 1,
                  "message": "v.0 cannot be greater than 0",
                },
              ],
              "v.1": Array [
                Object {
                  "field": "v.1",
                  "fieldValue": 2,
                  "message": "v.1 cannot be greater than 0",
                },
              ],
            }
          `);
          expect(errors).toMatchInlineSnapshot(`
            Array [
              Object {
                "field": "v.0",
                "fieldValue": 1,
                "message": "v.0 cannot be greater than 0",
              },
              Object {
                "field": "v.1",
                "fieldValue": 2,
                "message": "v.1 cannot be greater than 0",
              },
            ]
          `);
          done();
        },
      );
    });

    it('will merge top validation', () => {
      const obj = {
        value: '',
        test: [
          {
            name: 'aa',
          },
        ],
      };

      const descriptor: Rules = {
        test: {
          type: 'array',
          min: 2,
          required: true,
          message: '至少两项',
          defaultField: [
            {
              type: 'object',
              required: true,
              message: 'test 必须有',
              fields: {
                name: {
                  type: 'string',
                  required: true,
                  message: 'name 必须有',
                },
              },
            },
          ],
        },
      };

      new Schema(descriptor).validate(obj, errors => {
        expect(errors).toMatchInlineSnapshot(`
          Array [
            Object {
              "field": "test",
              "fieldValue": Array [
                Object {
                  "name": "aa",
                },
              ],
              "message": "至少两项",
            },
          ]
        `);
      });
    });

    it('array & required works', done => {
      const descriptor: Rules = {
        testArray: {
          type: 'array',
          required: true,
          defaultField: [{ type: 'string' }],
        },
      };
      const record = {
        testArray: [],
      };
      const validator = new Schema(descriptor);
      validator.validate(record, (errors, fields) => {
        done();
      });
    });

    it('deep object all values validation', done => {
      new Schema({
        v: {
          required: true,
          type: 'object',
          defaultField: [{ type: 'string' }],
        },
      }).validate(
        {
          v: {
            a: 1,
            b: 'c',
          },
        },
        errors => {
          expect(errors.length).toBe(1);
          expect(errors[0].message).toBe('v.a is not a string');
          done();
        },
      );
    });
  });
});


================================================
FILE: __tests__/enum.spec.ts
================================================
import Schema from '../src';

describe('enum', () => {
  it('run validation on `false`', done => {
    new Schema({
      v: {
        type: 'enum',
        enum: [true],
      },
    }).validate(
      {
        v: false,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v must be one of true');
        done();
      },
    );
  });
});


================================================
FILE: __tests__/messages.spec.ts
================================================
import Schema, { ValidateMessages } from '../src';

describe('messages', () => {
  it('can call messages', done => {
    const messages = {
      required(f) {
        return `${f} required!`;
      },
    };
    const schema = new Schema({
      v: {
        required: true,
      },
      v2: {
        type: 'array',
      },
    });
    schema.messages(messages);
    schema.validate(
      {
        v: '',
        v2: '1',
      },
      errors => {
        expect(errors.length).toBe(2);
        expect(errors[0].message).toBe('v required!');
        expect(errors[1].message).toBe('v2 is not an array');
        expect(Object.keys(messages).length).toBe(1);
        done();
      },
    );
  });

  it('can use options.messages', done => {
    const messages = {
      required(f) {
        return `${f} required!`;
      },
    };
    const schema = new Schema({
      v: {
        required: true,
      },
      v2: {
        type: 'array',
      },
    });
    schema.validate(
      {
        v: '',
        v2: '1',
      },
      {
        messages,
      },
      errors => {
        expect(errors.length).toBe(2);
        expect(errors[0].message).toBe('v required!');
        expect(errors[1].message).toBe('v2 is not an array');
        expect(Object.keys(messages).length).toBe(1);
        done();
      },
    );
  });

  it('messages with parameters', done => {
    const messages = {
      required: 'Field %s required!',
    };
    const schema = new Schema({
      v: {
        required: true,
      },
    });
    schema.messages(messages);
    schema.validate(
      {
        v: '',
      },
      errors => {
        expect(errors).toBeTruthy();
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('Field v required!');
        expect(Object.keys(messages).length).toBe(1);
        done();
      },
    );
  });

  it('messages can be without parameters', done => {
    const messages = {
      required: 'required!',
    };
    const schema = new Schema({
      v: {
        required: true,
      },
    });
    schema.messages(messages);
    schema.validate(
      {
        v: '',
      },
      errors => {
        expect(errors).toBeTruthy();
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('required!');
        expect(Object.keys(messages).length).toBe(1);
        expect(messages.required).toBe('required!');
        done();
      },
    );
  });

  it('message can be a function', done => {
    const message = 'this is a function';
    new Schema({
      v: {
        required: true,
        message: () => message,
      },
    }).validate(
      {
        v: '', // provide empty value, this will trigger the message.
      },
      errors => {
        expect(errors).toBeTruthy();
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe(message);
        done();
      },
    );
  });
});


================================================
FILE: __tests__/number.spec.ts
================================================
import Schema from '../src';

describe('number', () => {
  it('works', done => {
    new Schema({
      v: {
        type: 'number',
      },
    }).validate(
      {
        v: '1',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is not a number');
        done();
      },
    );
  });

  it('works for no-required', done => {
    new Schema({
      v: {
        type: 'number',
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('works for no-required in case of empty string', done => {
    new Schema({
      v: {
        type: 'number',
        required: false,
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('works for required', done => {
    new Schema({
      v: {
        type: 'number',
        required: true,
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('transform does not change value', done => {
    const value = {
      v: '1',
    };
    new Schema({
      v: {
        type: 'number',
        transform: Number,
      },
    }).validate(value, (errors, data) => {
      expect(data).toEqual({
        v: 1,
      });
      expect(value.v).toBe('1');
      expect(errors).toBeFalsy();
      done();
    });
  });

  it('return transformed value in promise.then', done => {
    const value = {
      v: '1',
    };
    new Schema({
      v: {
        type: 'number',
        transform: Number,
      },
    })
      .validate(value, errors => {
        expect(value.v).toBe('1');
        expect(errors).toBeFalsy();
      })
      .then(source => {
        expect(source).toEqual({
          v: 1,
        });
        done();
      });
  });
});


================================================
FILE: __tests__/object.spec.ts
================================================
import Schema from '../src';

describe('object', () => {
  it('works for the required object with fields in case of empty string', done => {
    new Schema({
      v: {
        type: 'object',
        required: true,
        fields: {},
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is not an object');
        done();
      },
    );
  });
});


================================================
FILE: __tests__/pattern.spec.ts
================================================
import Schema from '../src';

describe('pattern', () => {
  it('works for non-required empty string', done => {
    new Schema({
      v: {
        pattern: /^\d+$/,
        message: 'haha',
      },
    }).validate(
      {
        // useful for web, input's value defaults to ''
        v: '',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('work for non-required empty string with string regexp', done => {
    new Schema({
      v: {
        pattern: '^\\d+$',
        message: 'haha',
      },
    }).validate(
      {
        // useful for web, input's value defaults to ''
        v: 's',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('haha');
        done();
      },
    );
  });

  it('works for required empty string', done => {
    new Schema({
      v: {
        pattern: /^\d+$/,
        message: 'haha',
        required: true,
      },
    }).validate(
      {
        // useful for web, input's value defaults to ''
        v: '',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('haha');
        done();
      },
    );
  });

  it('works for non-required null', done => {
    new Schema({
      v: {
        pattern: /^\d+$/,
        message: 'haha',
      },
    }).validate(
      {
        v: null,
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for non-required undefined', done => {
    new Schema({
      v: {
        pattern: /^\d+$/,
        message: 'haha',
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works', done => {
    new Schema({
      v: {
        pattern: /^\d+$/,
        message: 'haha',
      },
    }).validate(
      {
        v: ' ',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('haha');
        done();
      },
    );
  });

  it('works for RegExp with global flag', done => {
    const schema = new Schema({
      v: {
        pattern: /global/g,
        message: 'haha',
      },
    });

    schema.validate(
      {
        v: 'globalflag',
      },
      errors => {
        expect(errors).toBe(null);
      },
    );

    schema.validate(
      {
        v: 'globalflag',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });
});


================================================
FILE: __tests__/promise.spec.ts
================================================
import Schema from '../src';

describe('asyncValidator', () => {
  it('works', done => {
    new Schema({
      v: [
        {
          asyncValidator(rule, value) {
            return Promise.reject(new Error('e1'));
          },
        },
        {
          asyncValidator(rule, value) {
            return Promise.reject(new Error('e2'));
          },
        },
      ],
      v2: [
        {
          asyncValidator(rule, value) {
            return Promise.reject(new Error('e3'));
          },
        },
      ],
    }).validate(
      {
        v: 2,
      },
      errors => {
        expect(errors.length).toBe(3);
        expect(errors[0].message).toBe('e1');
        expect(errors[1].message).toBe('e2');
        expect(errors[2].message).toBe('e3');
        done();
      },
    );
  });

  it('first works', done => {
    new Schema({
      v: [
        {
          asyncValidator(rule, value) {
            return Promise.reject(new Error('e1'));
          },
        },
        {
          asyncValidator(rule, value) {
            return Promise.reject(new Error('e2'));
          },
        },
      ],
      v2: [
        {
          asyncValidator(rule, value) {
            return Promise.reject(new Error('e3'));
          },
        },
      ],
    }).validate(
      {
        v: 2,
        v2: 1,
      },
      {
        first: true,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('e1');
        done();
      },
    );
  });

  describe('firstFields', () => {
    it('works for true', done => {
      new Schema({
        v: [
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e1'));
            },
          },
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e2'));
            },
          },
        ],

        v2: [
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e3'));
            },
          },
        ],
        v3: [
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e4'));
            },
          },
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e5'));
            },
          },
        ],
      }).validate(
        {
          v: 1,
          v2: 1,
          v3: 1,
        },
        {
          firstFields: true,
        },
        errors => {
          expect(errors.length).toBe(3);
          expect(errors[0].message).toBe('e1');
          expect(errors[1].message).toBe('e3');
          expect(errors[2].message).toBe('e4');
          done();
        },
      );
    });

    it('works for array', done => {
      new Schema({
        v: [
          {
            asyncValidator: (rule, value) => {
              return Promise.reject(new Error('e1'));
            },
          },
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e2'));
            },
          },
        ],

        v2: [
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e3'));
            },
          },
        ],
        v3: [
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e4'));
            },
          },
          {
            asyncValidator(rule, value) {
              return Promise.reject(new Error('e5'));
            },
          },
        ],
        v4: [
          {
            asyncValidator: () =>
              new Promise((resolve, reject) => {
                setTimeout(resolve, 100);
              }),
          },
          {
            asyncValidator: () =>
              new Promise((resolve, reject) => {
                setTimeout(() => reject(new Error('e6')), 100);
              }),
          },
          {
            asyncValidator: () =>
              new Promise((resolve, reject) => {
                setTimeout(() => reject(new Error('')), 100);
              }),
          },
        ],
      }).validate(
        {
          v: 1,
          v2: 1,
          v3: 1,
        },
        {
          firstFields: ['v'],
        },
        errors => {
          expect(errors.length).toBe(6);
          expect(errors[0].message).toBe('e1');
          expect(errors[1].message).toBe('e3');
          expect(errors[2].message).toBe('e4');
          expect(errors[3].message).toBe('e5');
          expect(errors[4].message).toBe('e6');
          expect(errors[5].message).toBe('');
          done();
        },
      );
    });
    it("Whether to remove the 'Uncaught (in promise)' warning", async () => {
      let allCorrect = true;
      try {
        await new Schema({
          async: {
            asyncValidator(rule, value) {
              return new Promise((resolve, reject) => {
                setTimeout(() => {
                  reject([
                    new Error(
                      typeof rule.message === 'function'
                        ? rule.message()
                        : rule.message,
                    ),
                  ]);
                }, 100);
              });
            },
            message: 'async fails',
          },
        }).validate({
          v: 1,
        });
      } catch ({ errors }) {
        allCorrect = errors.length === 1;
      }
      expect(allCorrect).toBe(true);
    });
  });
});


================================================
FILE: __tests__/required.spec.ts
================================================
import Schema from '../src';
const required = true;

describe('required', () => {
  it('works for array required=true', done => {
    new Schema({
      v: [
        {
          required,
          message: 'no',
        },
      ],
    }).validate(
      {
        v: [],
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('no');
        done();
      },
    );
  });

  it('works for array required=true & custom message', done => {
    // allow custom message
    new Schema({
      v: [
        {
          required,
          message: 'no',
        },
      ],
    }).validate(
      {
        v: [1],
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('works for array required=false', done => {
    new Schema({
      v: {
        required: false,
      },
    }).validate(
      {
        v: [],
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('works for string required=true', done => {
    new Schema({
      v: {
        required,
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for string required=false', done => {
    new Schema({
      v: {
        required: false,
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('works for number required=true', done => {
    new Schema({
      v: {
        required,
      },
    }).validate(
      {
        v: 1,
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('works for number required=false', done => {
    new Schema({
      v: {
        required: false,
      },
    }).validate(
      {
        v: 1,
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('works for null required=true', done => {
    new Schema({
      v: {
        required,
      },
    }).validate(
      {
        v: null,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for null required=false', done => {
    new Schema({
      v: {
        required: false,
      },
    }).validate(
      {
        v: null,
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('works for undefined required=true', done => {
    new Schema({
      v: {
        required,
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for undefined required=false', done => {
    new Schema({
      v: {
        required: false,
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors).toBeFalsy();
        done();
      },
    );
  });

  it('should support empty string message', done => {
    new Schema({
      v: {
        required,
        message: '',
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('');
        done();
      },
    );
  });
});


================================================
FILE: __tests__/string.spec.ts
================================================
import Schema from '../src';

describe('string', () => {
  it('works for none require', done => {
    let data = {
      v: '',
    };
    new Schema({
      v: {
        type: 'string',
      },
    }).validate(data, (errors, d) => {
      expect(errors).toBe(null);
      expect(d).toEqual(data);
      done();
    });
  });

  it('works for empty string', done => {
    new Schema({
      v: {
        required: true,
        type: 'string',
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for undefined string', done => {
    new Schema({
      v: {
        required: true,
        type: 'string',
      },
    }).validate(
      {
        v: undefined,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for null string', done => {
    new Schema({
      v: {
        required: true,
        type: 'string',
      },
    }).validate(
      {
        v: null,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for message', done => {
    new Schema({
      v: {
        required: true,
        type: 'string',
        message: 'haha',
      },
    }).validate(
      {
        v: null,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('haha');
        done();
      },
    );
  });

  it('works for none empty', done => {
    new Schema({
      v: {
        required: true,
        type: 'string',
        message: 'haha',
      },
    }).validate(
      {
        v: ' ',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for whitespace empty', done => {
    new Schema({
      v: {
        required: true,
        type: 'string',
        whitespace: true,
        message: 'haha',
      },
    }).validate(
      {
        v: ' ',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('haha');
        done();
      },
    );
  });
});


================================================
FILE: __tests__/unicode.spec.ts
================================================
import Schema from '../src';

describe('unicode', () => {
  it('works for unicode U+0000 to U+FFFF ', done => {
    new Schema({
      v: {
        type: 'string',
        len: 4,
      },
    }).validate(
      {
        v: '吉吉吉吉',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for unicode gt U+FFFF ', done => {
    new Schema({
      v: {
        type: 'string',
        len: 4, // 原来length属性应该为8,更正之后应该为4
      },
    }).validate(
      {
        v: '𠮷𠮷𠮷𠮷',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('Rich Text Format', done => {
    new Schema({
      v: {
        type: 'string',
        len: 2,
      },
    }).validate(
      {
        v: '💩💩',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });
});


================================================
FILE: __tests__/url.spec.ts
================================================
import Schema from '../src';

describe('url', () => {
  it('works for empty string', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for ip url', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: 'http://10.218.136.29/talent-tree/src/index.html',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for required empty string', done => {
    new Schema({
      v: {
        type: 'url',
        required: true,
      },
    }).validate(
      {
        v: '',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is required');
        done();
      },
    );
  });

  it('works for type url', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: 'http://www.taobao.com',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for type url has query', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: 'http://www.taobao.com/abc?a=a',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for type url has hash', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: 'http://www.taobao.com/abc#!abc',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for type url has query and has', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: 'http://www.taobao.com/abc?abc=%23&b=a~c#abc',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for type url has multi hyphen', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: 'https://www.tao---bao.com',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });

  it('works for type not a valid url', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: 'http://www.taobao.com/abc?abc=%23&b=  a~c#abc    ',
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('v is not a valid url');
        done();
      },
    );
  });

  it('support skip schema', done => {
    new Schema({
      v: {
        type: 'url',
      },
    }).validate(
      {
        v: '//g.cn',
      },
      errors => {
        expect(errors).toBe(null);
        done();
      },
    );
  });
});


================================================
FILE: __tests__/validator.spec.ts
================================================
import Schema from '../src';

describe('validator', () => {
  it('works', done => {
    new Schema({
      v: [
        {
          validator(rule, value, callback) {
            callback(new Error('e1'));
          },
        },
        {
          validator(rule, value, callback) {
            callback(new Error('e2'));
          },
        },
      ],
      v2: [
        {
          validator(rule, value, callback) {
            callback(new Error('e3'));
          },
        },
      ],
      v3: [
        {
          validator() {
            return false;
          },
        },
        {
          validator() {
            return new Error('e5');
          },
        },
        {
          validator() {
            return false;
          },
          message: 'e6',
        },
        {
          validator() {
            return true;
          },
        },
        // Customize with empty message
        {
          validator() {
            return false;
          },
          message: '',
        },
      ],
    }).validate(
      {
        v: 2,
      },
      errors => {
        expect(errors.length).toBe(7);
        expect(errors[0].message).toBe('e1');
        expect(errors[1].message).toBe('e2');
        expect(errors[2].message).toBe('e3');
        expect(errors[3].message).toBe('v3 fails');
        expect(errors[4].message).toBe('e5');
        expect(errors[5].message).toBe('e6');
        expect(errors[6].message).toBe('');
        done();
      },
    );
  });

  it('first works', done => {
    new Schema({
      v: [
        {
          validator(rule, value, callback) {
            callback(new Error('e1'));
          },
        },
        {
          validator(rule, value, callback) {
            callback(new Error('e2'));
          },
        },
      ],
      v2: [
        {
          validator(rule, value, callback) {
            callback(new Error('e3'));
          },
        },
      ],
    }).validate(
      {
        v: 2,
        v2: 1,
      },
      {
        first: true,
      },
      errors => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('e1');
        done();
      },
    );
  });

  describe('firstFields', () => {
    it('works for true', done => {
      new Schema({
        v: [
          {
            validator(rule, value, callback) {
              callback(new Error('e1'));
            },
          },
          {
            validator(rule, value, callback) {
              callback(new Error('e2'));
            },
          },
        ],

        v2: [
          {
            validator(rule, value, callback) {
              callback(new Error('e3'));
            },
          },
        ],
        v3: [
          {
            validator(rule, value, callback) {
              callback(new Error('e4'));
            },
          },
          {
            validator(rule, value, callback) {
              callback(new Error('e5'));
            },
          },
        ],
      }).validate(
        {
          v: 1,
          v2: 1,
          v3: 1,
        },
        {
          firstFields: true,
        },
        errors => {
          expect(errors.length).toBe(3);
          expect(errors[0].message).toBe('e1');
          expect(errors[1].message).toBe('e3');
          expect(errors[2].message).toBe('e4');
          done();
        },
      );
    });

    it('works for array', done => {
      new Schema({
        v: [
          {
            validator(rule, value, callback) {
              callback(new Error('e1'));
            },
          },
          {
            validator(rule, value, callback) {
              callback(new Error('e2'));
            },
          },
        ],

        v2: [
          {
            validator(rule, value, callback) {
              callback(new Error('e3'));
            },
          },
        ],
        v3: [
          {
            validator(rule, value, callback) {
              callback(new Error('e4'));
            },
          },
          {
            validator(rule, value, callback) {
              callback(new Error('e5'));
            },
          },
        ],
      }).validate(
        {
          v: 1,
          v2: 1,
          v3: 1,
        },
        {
          firstFields: ['v'],
        },
        errors => {
          expect(errors.length).toBe(4);
          expect(errors[0].message).toBe('e1');
          expect(errors[1].message).toBe('e3');
          expect(errors[2].message).toBe('e4');
          expect(errors[3].message).toBe('e5');
          done();
        },
      );
    });
  });

  describe('promise api', () => {
    it('works', done => {
      new Schema({
        v: [
          {
            validator(rule, value, callback) {
              callback(new Error('e1'));
            },
          },
          {
            validator(rule, value, callback) {
              callback(new Error('e2'));
            },
          },
        ],
        v2: [
          {
            validator(rule, value, callback) {
              callback(new Error('e3'));
            },
          },
        ],
        v3: [
          {
            validator() {
              return false;
            },
          },
          {
            validator() {
              return new Error('e5');
            },
          },
          {
            validator() {
              return false;
            },
            message: 'e6',
          },
          {
            validator() {
              return true;
            },
          },
        ],
      })
        .validate({
          v: 2,
        })
        .catch(({ errors, fields }) => {
          expect(errors.length).toBe(6);
          expect(errors[0].message).toBe('e1');
          expect(errors[1].message).toBe('e2');
          expect(errors[2].message).toBe('e3');
          expect(errors[3].message).toBe('v3 fails');
          expect(errors[4].message).toBe('e5');
          expect(errors[5].message).toBe('e6');
          expect(fields.v[0].fieldValue).toBe(2);
          expect(fields).toMatchInlineSnapshot(`
            Object {
              "v": Array [
                [Error: e1],
                [Error: e2],
              ],
              "v2": Array [
                [Error: e3],
              ],
              "v3": Array [
                Object {
                  "field": "v3",
                  "fieldValue": undefined,
                  "message": "v3 fails",
                },
                Object {
                  "field": "v3",
                  "fieldValue": undefined,
                  "message": "e5",
                },
                Object {
                  "field": "v3",
                  "fieldValue": undefined,
                  "message": "e6",
                },
              ],
            }
          `);
          done();
        });
    });

    it('first works', done => {
      new Schema({
        v: [
          {
            validator(rule, value, callback) {
              callback(new Error('e1'));
            },
          },
          {
            validator(rule, value, callback) {
              callback(new Error('e2'));
            },
          },
        ],
        v2: [
          {
            validator(rule, value, callback) {
              callback(new Error('e3'));
            },
          },
        ],
      })
        .validate(
          {
            v: 2,
            v2: 1,
          },
          {
            first: true,
          },
        )
        .catch(({ errors }) => {
          expect(errors.length).toBe(1);
          expect(errors[0].message).toBe('e1');
          done();
        });
    });

    describe('firstFields', () => {
      it('works for true', done => {
        new Schema({
          v: [
            {
              validator(rule, value, callback) {
                callback(new Error('e1'));
              },
            },
            {
              validator(rule, value, callback) {
                callback(new Error('e2'));
              },
            },
          ],

          v2: [
            {
              validator(rule, value, callback) {
                callback(new Error('e3'));
              },
            },
          ],
          v3: [
            {
              validator(rule, value, callback) {
                callback(new Error('e4'));
              },
            },
            {
              validator(rule, value, callback) {
                callback(new Error('e5'));
              },
            },
          ],
        })
          .validate(
            {
              v: 1,
              v2: 1,
              v3: 1,
            },
            {
              firstFields: true,
            },
          )
          .catch(({ errors }) => {
            expect(errors.length).toBe(3);
            expect(errors[0].message).toBe('e1');
            expect(errors[1].message).toBe('e3');
            expect(errors[2].message).toBe('e4');
            done();
          });
      });

      it('works for array', done => {
        new Schema({
          v: [
            {
              validator(rule, value, callback) {
                callback(new Error('e1'));
              },
            },
            {
              validator(rule, value, callback) {
                callback(new Error('e2'));
              },
            },
          ],

          v2: [
            {
              validator(rule, value, callback) {
                callback(new Error('e3'));
              },
            },
          ],
          v3: [
            {
              validator(rule, value, callback) {
                callback(new Error('e4'));
              },
            },
            {
              validator(rule, value, callback) {
                callback(new Error('e5'));
              },
            },
          ],
        })
          .validate(
            {
              v: 1,
              v2: 1,
              v3: 1,
            },
            {
              firstFields: ['v'],
            },
          )
          .catch(({ errors }) => {
            expect(errors.length).toBe(4);
            expect(errors[0].message).toBe('e1');
            expect(errors[1].message).toBe('e3');
            expect(errors[2].message).toBe('e4');
            expect(errors[3].message).toBe('e5');
            done();
          });
      });

      it('works for no rules fields', done => {
        new Schema({
          v: [],
          v2: [],
        })
          .validate({
            v: 2,
            v2: 1,
          })
          .then(source => {
            expect(source).toMatchObject({ v: 2, v2: 1 });
            done();
          });
      });
    });
  });

  it('custom validate function throw error', done => {
    new Schema({
      v: [
        {
          validator(rule, value, callback) {
            throw new Error('something wrong');
          },
        },
      ],
    })
      .validate(
        { v: '' },
        {
          suppressValidatorError: true,
        },
      )
      .catch(({ errors }) => {
        expect(errors.length).toBe(1);
        expect(errors[0].message).toBe('something wrong');
        done();
      });
  });
});


================================================
FILE: package.json
================================================
{
  "name": "async-validator",
  "version": "4.2.5",
  "description": "validate form asynchronous",
  "typings": "typings/index.d.ts",
  "keywords": [
    "validator",
    "validate",
    "async"
  ],
  "homepage": "https://github.com/yiminghe/async-validator",
  "author": "yiminghe@gmail.com",
  "repository": {
    "type": "git",
    "url": "git@github.com:yiminghe/async-validator.git"
  },
  "@pika/pack": {
    "pipeline": [
      [
        "pika-plugin-ts-types",
        {
          "args": [
            "--rootDir",
            "src"
          ]
        }
      ],
      [
        "pika-plugin-build-web-babel",
        {
          "format": "cjs"
        }
      ],
      [
        "pika-plugin-build-web-babel"
      ]
    ]
  },
  "jest": {
    "collectCoverageFrom": [
      "src/*"
    ],
    "testMatch": [
      "**/__tests__/**/*.spec.[j|t]s?(x)"
    ]
  },
  "bugs": {
    "url": "https://github.com/yiminghe/async-validator/issues"
  },
  "license": "MIT",
  "config": {
    "port": 8010
  },
  "scripts": {
    "test-url": "yarn ts-node tests/url.ts",
    "lint-staged": "lint-staged",
    "prettier": "prettier --write \"{src,__tests__,tests}/**/*.{js,tsx,ts}\"",
    "pub": "np --no-cleanup --no-publish --no-release-draft && npm run build && cd pkg && npm publish",
    "build": "pika-pack build",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand",
    "coverage": "jest --coverage && cat ./coverage/lcov.info | coveralls",
    "coverage:gha": "jest --coverage",
    "version": "npm run build"
  },
  "devDependencies": {
    "@babel/core": "^7.15.0",
    "@babel/node": "^7.14.9",
    "@babel/preset-env": "^7.8.7",
    "@babel/preset-typescript": "^7.13.0",
    "@pika/pack": "^0.5.0",
    "@types/jest": "27.x",
    "babel-jest": "27.x",
    "coveralls": "^2.13.1",
    "jest": "27.x",
    "lint-staged": "^7.2.0",
    "np": "7.x",
    "pika-plugin-build-web-babel": "^0.10.0",
    "pika-plugin-ts-types": "0.1.x",
    "pre-commit": "^1.2.2",
    "prettier": "^1.11.1",
    "ts-node": "^10.8.1",
    "typescript": "^4.3.2"
  },
  "lint-staged": {
    "*.{tsx,js,jsx,ts}": [
      "prettier --write",
      "git add"
    ]
  },
  "pre-commit": [
    "lint-staged"
  ],
  "packageManager": "yarn@3.2.2"
}


================================================
FILE: scripts/runTest.ts
================================================
import Schema, {
  ValidateCallback,
  ValidateFieldsError,
  Values,
} from '../src/index';

const callback: ValidateCallback = (errors, fields) => {
  if (errors === null) {
    const f: Values = fields;
    console.log('transformed values:', JSON.stringify(f));
  } else {
    const f: ValidateFieldsError = fields;
    console.log('validate error:', JSON.stringify(f));
  }
};

new Schema({
  v: {
    required: true,
    type: 'array',
    defaultField: [{ type: 'number', max: 0, transform: i => Number(i) }],
  },
}).validate(
  {
    v: ['1', '2'],
  },
  callback,
);


================================================
FILE: scripts/test.sh
================================================
NODE_ENV=test npx babel-node -x ".ts,.js,.jsx" scripts/runTest.ts

================================================
FILE: src/index.ts
================================================
import {
  format,
  complementError,
  asyncMap,
  warning,
  deepMerge,
  convertFieldsError,
} from './util';
import validators from './validator/index';
import { messages as defaultMessages, newMessages } from './messages';
import {
  InternalRuleItem,
  InternalValidateMessages,
  Rule,
  RuleItem,
  Rules,
  ValidateCallback,
  ValidateMessages,
  ValidateOption,
  Values,
  RuleValuePackage,
  ValidateError,
  ValidateFieldsError,
  SyncErrorType,
  ValidateResult,
} from './interface';

export * from './interface';

/**
 *  Encapsulates a validation schema.
 *
 *  @param descriptor An object declaring validation rules
 *  for this schema.
 */
class Schema {
  // ========================= Static =========================
  static register = function register(type: string, validator) {
    if (typeof validator !== 'function') {
      throw new Error(
        'Cannot register a validator by type, validator is not a function',
      );
    }
    validators[type] = validator;
  };

  static warning = warning;

  static messages = defaultMessages;

  static validators = validators;

  // ======================== Instance ========================
  rules: Record<string, RuleItem[]> = null;
  _messages: InternalValidateMessages = defaultMessages;

  constructor(descriptor: Rules) {
    this.define(descriptor);
  }

  define(rules: Rules) {
    if (!rules) {
      throw new Error('Cannot configure a schema with no rules');
    }
    if (typeof rules !== 'object' || Array.isArray(rules)) {
      throw new Error('Rules must be an object');
    }
    this.rules = {};

    Object.keys(rules).forEach(name => {
      const item: Rule = rules[name];
      this.rules[name] = Array.isArray(item) ? item : [item];
    });
  }

  messages(messages?: ValidateMessages) {
    if (messages) {
      this._messages = deepMerge(newMessages(), messages);
    }
    return this._messages;
  }

  validate(
    source: Values,
    option?: ValidateOption,
    callback?: ValidateCallback,
  ): Promise<Values>;
  validate(source: Values, callback: ValidateCallback): Promise<Values>;
  validate(source: Values): Promise<Values>;

  validate(source_: Values, o: any = {}, oc: any = () => {}): Promise<Values> {
    let source: Values = source_;
    let options: ValidateOption = o;
    let callback: ValidateCallback = oc;
    if (typeof options === 'function') {
      callback = options;
      options = {};
    }
    if (!this.rules || Object.keys(this.rules).length === 0) {
      if (callback) {
        callback(null, source);
      }
      return Promise.resolve(source);
    }

    function complete(results: (ValidateError | ValidateError[])[]) {
      let errors: ValidateError[] = [];
      let fields: ValidateFieldsError = {};

      function add(e: ValidateError | ValidateError[]) {
        if (Array.isArray(e)) {
          errors = errors.concat(...e);
        } else {
          errors.push(e);
        }
      }

      for (let i = 0; i < results.length; i++) {
        add(results[i]);
      }
      if (!errors.length) {
        callback(null, source);
      } else {
        fields = convertFieldsError(errors);
        (callback as (
          errors: ValidateError[],
          fields: ValidateFieldsError,
        ) => void)(errors, fields);
      }
    }

    if (options.messages) {
      let messages = this.messages();
      if (messages === defaultMessages) {
        messages = newMessages();
      }
      deepMerge(messages, options.messages);
      options.messages = messages;
    } else {
      options.messages = this.messages();
    }

    const series: Record<string, RuleValuePackage[]> = {};
    const keys = options.keys || Object.keys(this.rules);
    keys.forEach(z => {
      const arr = this.rules[z];
      let value = source[z];
      arr.forEach(r => {
        let rule: InternalRuleItem = r;
        if (typeof rule.transform === 'function') {
          if (source === source_) {
            source = { ...source };
          }
          value = source[z] = rule.transform(value);
        }
        if (typeof rule === 'function') {
          rule = {
            validator: rule,
          };
        } else {
          rule = { ...rule };
        }

        // Fill validator. Skip if nothing need to validate
        rule.validator = this.getValidationMethod(rule);
        if (!rule.validator) {
          return;
        }

        rule.field = z;
        rule.fullField = rule.fullField || z;
        rule.type = this.getType(rule);
        series[z] = series[z] || [];
        series[z].push({
          rule,
          value,
          source,
          field: z,
        });
      });
    });
    const errorFields = {};
    return asyncMap(
      series,
      options,
      (data, doIt) => {
        const rule = data.rule;
        let deep =
          (rule.type === 'object' || rule.type === 'array') &&
          (typeof rule.fields === 'object' ||
            typeof rule.defaultField === 'object');
        deep = deep && (rule.required || (!rule.required && data.value));
        rule.field = data.field;

        function addFullField(key: string, schema: RuleItem) {
          return {
            ...schema,
            fullField: `${rule.fullField}.${key}`,
            fullFields: rule.fullFields ? [...rule.fullFields, key] : [key],
          };
        }

        function cb(e: SyncErrorType | SyncErrorType[] = []) {
          let errorList = Array.isArray(e) ? e : [e];
          if (!options.suppressWarning && errorList.length) {
            Schema.warning('async-validator:', errorList);
          }
          if (errorList.length && rule.message !== undefined) {
            errorList = [].concat(rule.message);
          }

          // Fill error info
          let filledErrors = errorList.map(complementError(rule, source));

          if (options.first && filledErrors.length) {
            errorFields[rule.field] = 1;
            return doIt(filledErrors);
          }
          if (!deep) {
            doIt(filledErrors);
          } else {
            // if rule is required but the target object
            // does not exist fail at the rule level and don't
            // go deeper
            if (rule.required && !data.value) {
              if (rule.message !== undefined) {
                filledErrors = []
                  .concat(rule.message)
                  .map(complementError(rule, source));
              } else if (options.error) {
                filledErrors = [
                  options.error(
                    rule,
                    format(options.messages.required, rule.field),
                  ),
                ];
              }
              return doIt(filledErrors);
            }

            let fieldsSchema: Record<string, Rule> = {};
            if (rule.defaultField) {
              Object.keys(data.value).map(key => {
                fieldsSchema[key] = rule.defaultField;
              });
            }
            fieldsSchema = {
              ...fieldsSchema,
              ...data.rule.fields,
            };

            const paredFieldsSchema: Record<string, RuleItem[]> = {};

            Object.keys(fieldsSchema).forEach(field => {
              const fieldSchema = fieldsSchema[field];
              const fieldSchemaList = Array.isArray(fieldSchema)
                ? fieldSchema
                : [fieldSchema];
              paredFieldsSchema[field] = fieldSchemaList.map(
                addFullField.bind(null, field),
              );
            });
            const schema = new Schema(paredFieldsSchema);
            schema.messages(options.messages);
            if (data.rule.options) {
              data.rule.options.messages = options.messages;
              data.rule.options.error = options.error;
            }
            schema.validate(data.value, data.rule.options || options, errs => {
              const finalErrors = [];
              if (filledErrors && filledErrors.length) {
                finalErrors.push(...filledErrors);
              }
              if (errs && errs.length) {
                finalErrors.push(...errs);
              }
              doIt(finalErrors.length ? finalErrors : null);
            });
          }
        }

        let res: ValidateResult;
        if (rule.asyncValidator) {
          res = rule.asyncValidator(rule, data.value, cb, data.source, options);
        } else if (rule.validator) {
          try {
            res = rule.validator(rule, data.value, cb, data.source, options);
          } catch (error) {
            console.error?.(error);
            // rethrow to report error
            if (!options.suppressValidatorError) {
              setTimeout(() => {
                throw error;
              }, 0);
            }
            cb(error.message);
          }
          if (res === true) {
            cb();
          } else if (res === false) {
            cb(
              typeof rule.message === 'function'
                ? rule.message(rule.fullField || rule.field)
                : rule.message || `${rule.fullField || rule.field} fails`,
            );
          } else if (res instanceof Array) {
            cb(res);
          } else if (res instanceof Error) {
            cb(res.message);
          }
        }
        if (res && (res as Promise<void>).then) {
          (res as Promise<void>).then(
            () => cb(),
            e => cb(e),
          );
        }
      },
      results => {
        complete(results);
      },
      source,
    );
  }

  getType(rule: InternalRuleItem) {
    if (rule.type === undefined && rule.pattern instanceof RegExp) {
      rule.type = 'pattern';
    }
    if (
      typeof rule.validator !== 'function' &&
      rule.type &&
      !validators.hasOwnProperty(rule.type)
    ) {
      throw new Error(format('Unknown rule type %s', rule.type));
    }
    return rule.type || 'string';
  }

  getValidationMethod(rule: InternalRuleItem) {
    if (typeof rule.validator === 'function') {
      return rule.validator;
    }
    const keys = Object.keys(rule);
    const messageIndex = keys.indexOf('message');
    if (messageIndex !== -1) {
      keys.splice(messageIndex, 1);
    }
    if (keys.length === 1 && keys[0] === 'required') {
      return validators.required;
    }
    return validators[this.getType(rule)] || undefined;
  }
}

export default Schema;


================================================
FILE: src/interface.ts
================================================
// >>>>> Rule
// Modified from https://github.com/yiminghe/async-validator/blob/0d51d60086a127b21db76f44dff28ae18c165c47/src/index.d.ts
export type RuleType =
  | 'string'
  | 'number'
  | 'boolean'
  | 'method'
  | 'regexp'
  | 'integer'
  | 'float'
  | 'array'
  | 'object'
  | 'enum'
  | 'date'
  | 'url'
  | 'hex'
  | 'email'
  | 'pattern'
  | 'any';

export interface ValidateOption {
  // whether to suppress internal warning
  suppressWarning?: boolean;

  // whether to suppress validator error
  suppressValidatorError?: boolean;

  // when the first validation rule generates an error stop processed
  first?: boolean;

  // when the first validation rule of the specified field generates an error stop the field processed, 'true' means all fields.
  firstFields?: boolean | string[];

  messages?: Partial<ValidateMessages>;

  /** The name of rules need to be trigger. Will validate all rules if leave empty */
  keys?: string[];

  error?: (rule: InternalRuleItem, message: string) => ValidateError;
}

export type SyncErrorType = Error | string;
export type SyncValidateResult = boolean | SyncErrorType | SyncErrorType[];
export type ValidateResult = void | Promise<void> | SyncValidateResult;

export interface RuleItem {
  type?: RuleType; // default type is 'string'
  required?: boolean;
  pattern?: RegExp | string;
  min?: number; // Range of type 'string' and 'array'
  max?: number; // Range of type 'string' and 'array'
  len?: number; // Length of type 'string' and 'array'
  enum?: Array<string | number | boolean | null | undefined>; // possible values of type 'enum'
  whitespace?: boolean;
  fields?: Record<string, Rule>; // ignore when without required
  options?: ValidateOption;
  defaultField?: Rule; // 'object' or 'array' containing validation rules
  transform?: (value: Value) => Value;
  message?: string | ((a?: string) => string);
  asyncValidator?: (
    rule: InternalRuleItem,
    value: Value,
    callback: (error?: string | Error) => void,
    source: Values,
    options: ValidateOption,
  ) => void | Promise<void>;
  validator?: (
    rule: InternalRuleItem,
    value: Value,
    callback: (error?: string | Error) => void,
    source: Values,
    options: ValidateOption,
  ) => SyncValidateResult | void;
}

export type Rule = RuleItem | RuleItem[];

export type Rules = Record<string, Rule>;

/**
 *  Rule for validating a value exists in an enumerable list.
 *
 *  @param rule The validation rule.
 *  @param value The value of the field on the source object.
 *  @param source The source object being validated.
 *  @param errors An array of errors that this rule may add
 *  validation errors to.
 *  @param options The validation options.
 *  @param options.messages The validation messages.
 *  @param type Rule type
 */
export type ExecuteRule = (
  rule: InternalRuleItem,
  value: Value,
  source: Values,
  errors: string[],
  options: ValidateOption,
  type?: string,
) => void;

/**
 *  Performs validation for any type.
 *
 *  @param rule The validation rule.
 *  @param value The value of the field on the source object.
 *  @param callback The callback function.
 *  @param source The source object being validated.
 *  @param options The validation options.
 *  @param options.messages The validation messages.
 */
export type ExecuteValidator = (
  rule: InternalRuleItem,
  value: Value,
  callback: (error?: string[]) => void,
  source: Values,
  options: ValidateOption,
) => void;

// >>>>> Message
type ValidateMessage<T extends any[] = unknown[]> =
  | string
  | ((...args: T) => string);
type FullField = string | undefined;
type EnumString = string | undefined;
type Pattern = string | RegExp | undefined;
type Range = number | undefined;
type Type = string | undefined;

export interface ValidateMessages {
  default?: ValidateMessage;
  required?: ValidateMessage<[FullField]>;
  enum?: ValidateMessage<[FullField, EnumString]>;
  whitespace?: ValidateMessage<[FullField]>;
  date?: {
    format?: ValidateMessage;
    parse?: ValidateMessage;
    invalid?: ValidateMessage;
  };
  types?: {
    string?: ValidateMessage<[FullField, Type]>;
    method?: ValidateMessage<[FullField, Type]>;
    array?: ValidateMessage<[FullField, Type]>;
    object?: ValidateMessage<[FullField, Type]>;
    number?: ValidateMessage<[FullField, Type]>;
    date?: ValidateMessage<[FullField, Type]>;
    boolean?: ValidateMessage<[FullField, Type]>;
    integer?: ValidateMessage<[FullField, Type]>;
    float?: ValidateMessage<[FullField, Type]>;
    regexp?: ValidateMessage<[FullField, Type]>;
    email?: ValidateMessage<[FullField, Type]>;
    url?: ValidateMessage<[FullField, Type]>;
    hex?: ValidateMessage<[FullField, Type]>;
  };
  string?: {
    len?: ValidateMessage<[FullField, Range]>;
    min?: ValidateMessage<[FullField, Range]>;
    max?: ValidateMessage<[FullField, Range]>;
    range?: ValidateMessage<[FullField, Range, Range]>;
  };
  number?: {
    len?: ValidateMessage<[FullField, Range]>;
    min?: ValidateMessage<[FullField, Range]>;
    max?: ValidateMessage<[FullField, Range]>;
    range?: ValidateMessage<[FullField, Range, Range]>;
  };
  array?: {
    len?: ValidateMessage<[FullField, Range]>;
    min?: ValidateMessage<[FullField, Range]>;
    max?: ValidateMessage<[FullField, Range]>;
    range?: ValidateMessage<[FullField, Range, Range]>;
  };
  pattern?: {
    mismatch?: ValidateMessage<[FullField, Value, Pattern]>;
  };
}

export interface InternalValidateMessages extends ValidateMessages {
  clone: () => InternalValidateMessages;
}

// >>>>> Values
export type Value = any;
export type Values = Record<string, Value>;

// >>>>> Validate
export interface ValidateError {
  message?: string;
  fieldValue?: Value;
  field?: string;
}

export type ValidateFieldsError = Record<string, ValidateError[]>;

export type ValidateCallback = (
  errors: ValidateError[] | null,
  fields: ValidateFieldsError | Values,
) => void;

export interface RuleValuePackage {
  rule: InternalRuleItem;
  value: Value;
  source: Values;
  field: string;
}

export interface InternalRuleItem extends Omit<RuleItem, 'validator'> {
  field?: string;
  fullField?: string;
  fullFields?: string[];
  validator?: RuleItem['validator'] | ExecuteValidator;
}


================================================
FILE: src/messages.ts
================================================
import { InternalValidateMessages } from './interface';

export function newMessages(): InternalValidateMessages {
  return {
    default: 'Validation error on field %s',
    required: '%s is required',
    enum: '%s must be one of %s',
    whitespace: '%s cannot be empty',
    date: {
      format: '%s date %s is invalid for format %s',
      parse: '%s date could not be parsed, %s is invalid ',
      invalid: '%s date %s is invalid',
    },
    types: {
      string: '%s is not a %s',
      method: '%s is not a %s (function)',
      array: '%s is not an %s',
      object: '%s is not an %s',
      number: '%s is not a %s',
      date: '%s is not a %s',
      boolean: '%s is not a %s',
      integer: '%s is not an %s',
      float: '%s is not a %s',
      regexp: '%s is not a valid %s',
      email: '%s is not a valid %s',
      url: '%s is not a valid %s',
      hex: '%s is not a valid %s',
    },
    string: {
      len: '%s must be exactly %s characters',
      min: '%s must be at least %s characters',
      max: '%s cannot be longer than %s characters',
      range: '%s must be between %s and %s characters',
    },
    number: {
      len: '%s must equal %s',
      min: '%s cannot be less than %s',
      max: '%s cannot be greater than %s',
      range: '%s must be between %s and %s',
    },
    array: {
      len: '%s must be exactly %s in length',
      min: '%s cannot be less than %s in length',
      max: '%s cannot be greater than %s in length',
      range: '%s must be between %s and %s in length',
    },
    pattern: {
      mismatch: '%s value %s does not match pattern %s',
    },
    clone() {
      const cloned = JSON.parse(JSON.stringify(this));
      cloned.clone = this.clone;
      return cloned;
    },
  };
}

export const messages = newMessages();


================================================
FILE: src/rule/enum.ts
================================================
import { ExecuteRule } from '../interface';
import { format } from '../util';

const ENUM = 'enum' as const;

const enumerable: ExecuteRule = (rule, value, source, errors, options) => {
  rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : [];
  if (rule[ENUM].indexOf(value) === -1) {
    errors.push(
      format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', ')),
    );
  }
};

export default enumerable;


================================================
FILE: src/rule/index.ts
================================================
import required from './required';
import whitespace from './whitespace';
import type from './type';
import range from './range';
import enumRule from './enum';
import pattern from './pattern';

export default {
  required,
  whitespace,
  type,
  range,
  enum: enumRule,
  pattern,
};


================================================
FILE: src/rule/pattern.ts
================================================
import { ExecuteRule } from '../interface';
import { format } from '../util';

const pattern: ExecuteRule = (rule, value, source, errors, options) => {
  if (rule.pattern) {
    if (rule.pattern instanceof RegExp) {
      // if a RegExp instance is passed, reset `lastIndex` in case its `global`
      // flag is accidentally set to `true`, which in a validation scenario
      // is not necessary and the result might be misleading
      rule.pattern.lastIndex = 0;
      if (!rule.pattern.test(value)) {
        errors.push(
          format(
            options.messages.pattern.mismatch,
            rule.fullField,
            value,
            rule.pattern,
          ),
        );
      }
    } else if (typeof rule.pattern === 'string') {
      const _pattern = new RegExp(rule.pattern);
      if (!_pattern.test(value)) {
        errors.push(
          format(
            options.messages.pattern.mismatch,
            rule.fullField,
            value,
            rule.pattern,
          ),
        );
      }
    }
  }
};

export default pattern;


================================================
FILE: src/rule/range.ts
================================================
import { ExecuteRule } from '../interface';
import { format } from '../util';

const range: ExecuteRule = (rule, value, source, errors, options) => {
  const len = typeof rule.len === 'number';
  const min = typeof rule.min === 'number';
  const max = typeof rule.max === 'number';
  // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane)
  const spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
  let val = value;
  let key = null;
  const num = typeof value === 'number';
  const str = typeof value === 'string';
  const arr = Array.isArray(value);
  if (num) {
    key = 'number';
  } else if (str) {
    key = 'string';
  } else if (arr) {
    key = 'array';
  }
  // if the value is not of a supported type for range validation
  // the validation rule rule should use the
  // type property to also test for a particular type
  if (!key) {
    return false;
  }
  if (arr) {
    val = value.length;
  }
  if (str) {
    // 处理码点大于U+010000的文字length属性不准确的bug,如"𠮷𠮷𠮷".length !== 3
    val = value.replace(spRegexp, '_').length;
  }
  if (len) {
    if (val !== rule.len) {
      errors.push(format(options.messages[key].len, rule.fullField, rule.len));
    }
  } else if (min && !max && val < rule.min) {
    errors.push(format(options.messages[key].min, rule.fullField, rule.min));
  } else if (max && !min && val > rule.max) {
    errors.push(format(options.messages[key].max, rule.fullField, rule.max));
  } else if (min && max && (val < rule.min || val > rule.max)) {
    errors.push(
      format(options.messages[key].range, rule.fullField, rule.min, rule.max),
    );
  }
};

export default range;


================================================
FILE: src/rule/required.ts
================================================
import { ExecuteRule } from '../interface';
import { format, isEmptyValue } from '../util';

const required: ExecuteRule = (rule, value, source, errors, options, type) => {
  if (
    rule.required &&
    (!source.hasOwnProperty(rule.field) ||
      isEmptyValue(value, type || rule.type))
  ) {
    errors.push(format(options.messages.required, rule.fullField));
  }
};

export default required;


================================================
FILE: src/rule/type.ts
================================================
import { ExecuteRule, Value } from '../interface';
import { format } from '../util';
import required from './required';
import getUrlRegex from './url';
/* eslint max-len:0 */

const pattern = {
  // http://emailregex.com/
  email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/,
  // url: new RegExp(
  //   '^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
  //   'i',
  // ),
  hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i,
};

const types = {
  integer(value: Value) {
    return types.number(value) && parseInt(value, 10) === value;
  },
  float(value: Value) {
    return types.number(value) && !types.integer(value);
  },
  array(value: Value) {
    return Array.isArray(value);
  },
  regexp(value: Value) {
    if (value instanceof RegExp) {
      return true;
    }
    try {
      return !!new RegExp(value);
    } catch (e) {
      return false;
    }
  },
  date(value: Value) {
    return (
      typeof value.getTime === 'function' &&
      typeof value.getMonth === 'function' &&
      typeof value.getYear === 'function' &&
      !isNaN(value.getTime())
    );
  },
  number(value: Value) {
    if (isNaN(value)) {
      return false;
    }
    return typeof value === 'number';
  },
  object(value: Value) {
    return typeof value === 'object' && !types.array(value);
  },
  method(value: Value) {
    return typeof value === 'function';
  },
  email(value: Value) {
    return (
      typeof value === 'string' &&
      value.length <= 320 &&
      !!value.match(pattern.email)
    );
  },
  url(value: Value) {
    return (
      typeof value === 'string' &&
      value.length <= 2048 &&
      !!value.match(getUrlRegex())
    );
  },
  hex(value: Value) {
    return typeof value === 'string' && !!value.match(pattern.hex);
  },
};

const type: ExecuteRule = (rule, value, source, errors, options) => {
  if (rule.required && value === undefined) {
    required(rule, value, source, errors, options);
    return;
  }
  const custom = [
    'integer',
    'float',
    'array',
    'regexp',
    'object',
    'method',
    'email',
    'number',
    'date',
    'url',
    'hex',
  ];
  const ruleType = rule.type;
  if (custom.indexOf(ruleType) > -1) {
    if (!types[ruleType](value)) {
      errors.push(
        format(options.messages.types[ruleType], rule.fullField, rule.type),
      );
    }
    // straight typeof check
  } else if (ruleType && typeof value !== rule.type) {
    errors.push(
      format(options.messages.types[ruleType], rule.fullField, rule.type),
    );
  }
};

export default type;


================================================
FILE: src/rule/url.ts
================================================
// https://github.com/kevva/url-regex/blob/master/index.js
let urlReg: RegExp;

export default () => {
  if (urlReg) {
    return urlReg;
  }

  const word = '[a-fA-F\\d:]';
  const b = options =>
    options && options.includeBoundaries
      ? `(?:(?<=\\s|^)(?=${word})|(?<=${word})(?=\\s|$))`
      : '';

  const v4 =
    '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}';

  const v6seg = '[a-fA-F\\d]{1,4}';
  const v6 = `
(?:
(?:${v6seg}:){7}(?:${v6seg}|:)|                                    // 1:2:3:4:5:6:7::  1:2:3:4:5:6:7:8
(?:${v6seg}:){6}(?:${v4}|:${v6seg}|:)|                             // 1:2:3:4:5:6::    1:2:3:4:5:6::8   1:2:3:4:5:6::8  1:2:3:4:5:6::1.2.3.4
(?:${v6seg}:){5}(?::${v4}|(?::${v6seg}){1,2}|:)|                   // 1:2:3:4:5::      1:2:3:4:5::7:8   1:2:3:4:5::8    1:2:3:4:5::7:1.2.3.4
(?:${v6seg}:){4}(?:(?::${v6seg}){0,1}:${v4}|(?::${v6seg}){1,3}|:)| // 1:2:3:4::        1:2:3:4::6:7:8   1:2:3:4::8      1:2:3:4::6:7:1.2.3.4
(?:${v6seg}:){3}(?:(?::${v6seg}){0,2}:${v4}|(?::${v6seg}){1,4}|:)| // 1:2:3::          1:2:3::5:6:7:8   1:2:3::8        1:2:3::5:6:7:1.2.3.4
(?:${v6seg}:){2}(?:(?::${v6seg}){0,3}:${v4}|(?::${v6seg}){1,5}|:)| // 1:2::            1:2::4:5:6:7:8   1:2::8          1:2::4:5:6:7:1.2.3.4
(?:${v6seg}:){1}(?:(?::${v6seg}){0,4}:${v4}|(?::${v6seg}){1,6}|:)| // 1::              1::3:4:5:6:7:8   1::8            1::3:4:5:6:7:1.2.3.4
(?::(?:(?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:))             // ::2:3:4:5:6:7:8  ::2:3:4:5:6:7:8  ::8             ::1.2.3.4
)(?:%[0-9a-zA-Z]{1,})?                                             // %eth0            %1
`
    .replace(/\s*\/\/.*$/gm, '')
    .replace(/\n/g, '')
    .trim();

  // Pre-compile only the exact regexes because adding a global flag make regexes stateful
  const v46Exact = new RegExp(`(?:^${v4}$)|(?:^${v6}$)`);
  const v4exact = new RegExp(`^${v4}$`);
  const v6exact = new RegExp(`^${v6}$`);

  const ip = options =>
    options && options.exact
      ? v46Exact
      : new RegExp(
          `(?:${b(options)}${v4}${b(options)})|(?:${b(options)}${v6}${b(
            options,
          )})`,
          'g',
        );

  ip.v4 = (options?) =>
    options && options.exact
      ? v4exact
      : new RegExp(`${b(options)}${v4}${b(options)}`, 'g');
  ip.v6 = (options?) =>
    options && options.exact
      ? v6exact
      : new RegExp(`${b(options)}${v6}${b(options)}`, 'g');

  const protocol = `(?:(?:[a-z]+:)?//)`;
  const auth = '(?:\\S+(?::\\S*)?@)?';
  const ipv4 = ip.v4().source;
  const ipv6 = ip.v6().source;
  const host = '(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)';
  const domain =
    '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
  const tld = `(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))`;
  const port = '(?::\\d{2,5})?';
  const path = '(?:[/?#][^\\s"]*)?';
  const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}`;
  urlReg = new RegExp(`(?:^${regex}$)`, 'i');
  return urlReg;
};


================================================
FILE: src/rule/whitespace.ts
================================================
import { ExecuteRule } from '../interface';
import { format } from '../util';

/**
 *  Rule for validating whitespace.
 *
 *  @param rule The validation rule.
 *  @param value The value of the field on the source object.
 *  @param source The source object being validated.
 *  @param errors An array of errors that this rule may add
 *  validation errors to.
 *  @param options The validation options.
 *  @param options.messages The validation messages.
 */
const whitespace: ExecuteRule = (rule, value, source, errors, options) => {
  if (/^\s+$/.test(value) || value === '') {
    errors.push(format(options.messages.whitespace, rule.fullField));
  }
};

export default whitespace;


================================================
FILE: src/util.ts
================================================
/* eslint no-console:0 */

import {
  ValidateError,
  ValidateOption,
  RuleValuePackage,
  InternalRuleItem,
  SyncErrorType,
  RuleType,
  Value,
  Values,
} from './interface';

const formatRegExp = /%[sdj%]/g;

declare var ASYNC_VALIDATOR_NO_WARNING;

export let warning: (type: string, errors: SyncErrorType[]) => void = () => {};

// don't print warning message when in production env or node runtime
if (
  typeof process !== 'undefined' &&
  process.env &&
  process.env.NODE_ENV !== 'production' &&
  typeof window !== 'undefined' &&
  typeof document !== 'undefined'
) {
  warning = (type, errors) => {
    if (
      typeof console !== 'undefined' &&
      console.warn &&
      typeof ASYNC_VALIDATOR_NO_WARNING === 'undefined'
    ) {
      if (errors.every(e => typeof e === 'string')) {
        console.warn(type, errors);
      }
    }
  };
}

export function convertFieldsError(
  errors: ValidateError[],
): Record<string, ValidateError[]> {
  if (!errors || !errors.length) return null;
  const fields = {};
  errors.forEach(error => {
    const field = error.field;
    fields[field] = fields[field] || [];
    fields[field].push(error);
  });
  return fields;
}

export function format(
  template: ((...args: any[]) => string) | string,
  ...args: any[]
): string {
  let i = 0;
  const len = args.length;
  if (typeof template === 'function') {
    return template.apply(null, args);
  }
  if (typeof template === 'string') {
    let str = template.replace(formatRegExp, x => {
      if (x === '%%') {
        return '%';
      }
      if (i >= len) {
        return x;
      }
      switch (x) {
        case '%s':
          return String(args[i++]);
        case '%d':
          return (Number(args[i++]) as unknown) as string;
        case '%j':
          try {
            return JSON.stringify(args[i++]);
          } catch (_) {
            return '[Circular]';
          }
          break;
        default:
          return x;
      }
    });
    return str;
  }
  return template;
}

function isNativeStringType(type: string) {
  return (
    type === 'string' ||
    type === 'url' ||
    type === 'hex' ||
    type === 'email' ||
    type === 'date' ||
    type === 'pattern'
  );
}

export function isEmptyValue(value: Value, type?: string) {
  if (value === undefined || value === null) {
    return true;
  }
  if (type === 'array' && Array.isArray(value) && !value.length) {
    return true;
  }
  if (isNativeStringType(type) && typeof value === 'string' && !value) {
    return true;
  }
  return false;
}

export function isEmptyObject(obj: object) {
  return Object.keys(obj).length === 0;
}

function asyncParallelArray(
  arr: RuleValuePackage[],
  func: ValidateFunc,
  callback: (errors: ValidateError[]) => void,
) {
  const results: ValidateError[] = [];
  let total = 0;
  const arrLength = arr.length;

  function count(errors: ValidateError[]) {
    results.push(...(errors || []));
    total++;
    if (total === arrLength) {
      callback(results);
    }
  }

  arr.forEach(a => {
    func(a, count);
  });
}

function asyncSerialArray(
  arr: RuleValuePackage[],
  func: ValidateFunc,
  callback: (errors: ValidateError[]) => void,
) {
  let index = 0;
  const arrLength = arr.length;

  function next(errors: ValidateError[]) {
    if (errors && errors.length) {
      callback(errors);
      return;
    }
    const original = index;
    index = index + 1;
    if (original < arrLength) {
      func(arr[original], next);
    } else {
      callback([]);
    }
  }

  next([]);
}

function flattenObjArr(objArr: Record<string, RuleValuePackage[]>) {
  const ret: RuleValuePackage[] = [];
  Object.keys(objArr).forEach(k => {
    ret.push(...(objArr[k] || []));
  });
  return ret;
}

export class AsyncValidationError extends Error {
  errors: ValidateError[];
  fields: Record<string, ValidateError[]>;

  constructor(
    errors: ValidateError[],
    fields: Record<string, ValidateError[]>,
  ) {
    super('Async Validation Error');
    this.errors = errors;
    this.fields = fields;
  }
}

type ValidateFunc = (
  data: RuleValuePackage,
  doIt: (errors: ValidateError[]) => void,
) => void;

export function asyncMap(
  objArr: Record<string, RuleValuePackage[]>,
  option: ValidateOption,
  func: ValidateFunc,
  callback: (errors: ValidateError[]) => void,
  source: Values,
): Promise<Values> {
  if (option.first) {
    const pending = new Promise<Values>((resolve, reject) => {
      const next = (errors: ValidateError[]) => {
        callback(errors);
        return errors.length
          ? reject(new AsyncValidationError(errors, convertFieldsError(errors)))
          : resolve(source);
      };
      const flattenArr = flattenObjArr(objArr);
      asyncSerialArray(flattenArr, func, next);
    });
    pending.catch(e => e);
    return pending;
  }
  const firstFields =
    option.firstFields === true
      ? Object.keys(objArr)
      : option.firstFields || [];

  const objArrKeys = Object.keys(objArr);
  const objArrLength = objArrKeys.length;
  let total = 0;
  const results: ValidateError[] = [];
  const pending = new Promise<Values>((resolve, reject) => {
    const next = (errors: ValidateError[]) => {
      results.push.apply(results, errors);
      total++;
      if (total === objArrLength) {
        callback(results);
        return results.length
          ? reject(
              new AsyncValidationError(results, convertFieldsError(results)),
            )
          : resolve(source);
      }
    };
    if (!objArrKeys.length) {
      callback(results);
      resolve(source);
    }
    objArrKeys.forEach(key => {
      const arr = objArr[key];
      if (firstFields.indexOf(key) !== -1) {
        asyncSerialArray(arr, func, next);
      } else {
        asyncParallelArray(arr, func, next);
      }
    });
  });
  pending.catch(e => e);
  return pending;
}

function isErrorObj(
  obj: ValidateError | string | (() => string),
): obj is ValidateError {
  return !!(obj && (obj as ValidateError).message !== undefined);
}

function getValue(value: Values, path: string[]) {
  let v = value;
  for (let i = 0; i < path.length; i++) {
    if (v == undefined) {
      return v;
    }
    v = v[path[i]];
  }
  return v;
}

export function complementError(rule: InternalRuleItem, source: Values) {
  return (oe: ValidateError | (() => string) | string): ValidateError => {
    let fieldValue;
    if (rule.fullFields) {
      fieldValue = getValue(source, rule.fullFields);
    } else {
      fieldValue = source[(oe as any).field || rule.fullField];
    }
    if (isErrorObj(oe)) {
      oe.field = oe.field || rule.fullField;
      oe.fieldValue = fieldValue;
      return oe;
    }
    return {
      message: typeof oe === 'function' ? oe() : oe,
      fieldValue,
      field: ((oe as unknown) as ValidateError).field || rule.fullField,
    };
  };
}

export function deepMerge<T extends object>(target: T, source: Partial<T>): T {
  if (source) {
    for (const s in source) {
      if (source.hasOwnProperty(s)) {
        const value = source[s];
        if (typeof value === 'object' && typeof target[s] === 'object') {
          target[s] = {
            ...target[s],
            ...value,
          };
        } else {
          target[s] = value;
        }
      }
    }
  }
  return target;
}


================================================
FILE: src/validator/any.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const any: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
  }
  callback(errors);
};

export default any;


================================================
FILE: src/validator/array.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule/index';

const array: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if ((value === undefined || value === null) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options, 'array');
    if (value !== undefined && value !== null) {
      rules.type(rule, value, source, errors, options);
      rules.range(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default array;


================================================
FILE: src/validator/boolean.ts
================================================
import { isEmptyValue } from '../util';
import rules from '../rule';
import { ExecuteValidator } from '../interface';

const boolean: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (value !== undefined) {
      rules.type(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default boolean;


================================================
FILE: src/validator/date.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const date: ExecuteValidator = (rule, value, callback, source, options) => {
  // console.log('integer rule called %j', rule);
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  // console.log('validate on %s value', value);
  if (validate) {
    if (isEmptyValue(value, 'date') && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (!isEmptyValue(value, 'date')) {
      let dateObject;

      if (value instanceof Date) {
        dateObject = value;
      } else {
        dateObject = new Date(value);
      }

      rules.type(rule, dateObject, source, errors, options);
      if (dateObject) {
        rules.range(rule, dateObject.getTime(), source, errors, options);
      }
    }
  }
  callback(errors);
};

export default date;


================================================
FILE: src/validator/enum.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const ENUM = 'enum' as const;

const enumerable: ExecuteValidator = (
  rule,
  value,
  callback,
  source,
  options,
) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (value !== undefined) {
      rules[ENUM](rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default enumerable;


================================================
FILE: src/validator/float.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const floatFn: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (value !== undefined) {
      rules.type(rule, value, source, errors, options);
      rules.range(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default floatFn;


================================================
FILE: src/validator/index.ts
================================================
import string from './string';
import method from './method';
import number from './number';
import boolean from './boolean';
import regexp from './regexp';
import integer from './integer';
import float from './float';
import array from './array';
import object from './object';
import enumValidator from './enum';
import pattern from './pattern';
import date from './date';
import required from './required';
import type from './type';
import any from './any';

export default {
  string,
  method,
  number,
  boolean,
  regexp,
  integer,
  float,
  array,
  object,
  enum: enumValidator,
  pattern,
  date,
  url: type,
  hex: type,
  email: type,
  required,
  any,
};


================================================
FILE: src/validator/integer.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const integer: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (value !== undefined) {
      rules.type(rule, value, source, errors, options);
      rules.range(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default integer;


================================================
FILE: src/validator/method.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const method: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (value !== undefined) {
      rules.type(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default method;


================================================
FILE: src/validator/number.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const number: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (value === '') {
      value = undefined;
    }
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (value !== undefined) {
      rules.type(rule, value, source, errors, options);
      rules.range(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default number;


================================================
FILE: src/validator/object.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const object: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (value !== undefined) {
      rules.type(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default object;


================================================
FILE: src/validator/pattern.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const pattern: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value, 'string') && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (!isEmptyValue(value, 'string')) {
      rules.pattern(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default pattern;


================================================
FILE: src/validator/regexp.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const regexp: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options);
    if (!isEmptyValue(value)) {
      rules.type(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default regexp;


================================================
FILE: src/validator/required.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';

const required: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const type = Array.isArray(value) ? 'array' : typeof value;
  rules.required(rule, value, source, errors, options, type);
  callback(errors);
};

export default required;


================================================
FILE: src/validator/string.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const string: ExecuteValidator = (rule, value, callback, source, options) => {
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value, 'string') && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options, 'string');
    if (!isEmptyValue(value, 'string')) {
      rules.type(rule, value, source, errors, options);
      rules.range(rule, value, source, errors, options);
      rules.pattern(rule, value, source, errors, options);
      if (rule.whitespace === true) {
        rules.whitespace(rule, value, source, errors, options);
      }
    }
  }
  callback(errors);
};

export default string;


================================================
FILE: src/validator/type.ts
================================================
import { ExecuteValidator } from '../interface';
import rules from '../rule';
import { isEmptyValue } from '../util';

const type: ExecuteValidator = (rule, value, callback, source, options) => {
  const ruleType = rule.type;
  const errors: string[] = [];
  const validate =
    rule.required || (!rule.required && source.hasOwnProperty(rule.field));
  if (validate) {
    if (isEmptyValue(value, ruleType) && !rule.required) {
      return callback();
    }
    rules.required(rule, value, source, errors, options, ruleType);
    if (!isEmptyValue(value, ruleType)) {
      rules.type(rule, value, source, errors, options);
    }
  }
  callback(errors);
};

export default type;


================================================
FILE: tests/url.ts
================================================
import AsyncValidator from '../src';

const validator = new AsyncValidator({
  v: {
    type: 'url',
  },
});

for (var i = 1; i <= 1000; i++) {
  var time = Date.now();
  var attack_str = '//a.b' + 'c1'.repeat(i) + 'a';
  validator.validate({
    v: attack_str,
  });
  var time_cost = Date.now() - time;
  console.log(
    'attack_str.length: ' + attack_str.length + ': ' + time_cost + ' ms',
  );
}

if (false) {
  console.log('*'.repeat(10));

  for (var i = 1; i <= 50000; i++) {
    var time = Date.now();
    var attack_str = '//' + ':'.repeat(i * 10000) + '@';
    validator.validate({
      v: attack_str,
    });
    var time_cost = Date.now() - time;
    console.log(
      'attack_str.length: ' + attack_str.length + ': ' + time_cost + ' ms',
    );
  }
}


================================================
FILE: tsconfig.json
================================================
{
  "compilerOptions": {
    "outDir": "lib",
    "module": "commonjs",
    "target": "ES2020",
    "lib": [
      "es2016",
      "dom",
      "es5"
    ],
    "jsx": "preserve",
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "isolatedModules": true,
    "skipLibCheck": true,
    "declaration": true,
    "strictFunctionTypes": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "plugins": [
      {
        "transform": "@zerollup/ts-transform-paths"
      }
    ]
  },
  "include": [		
    "src/"
  ],
  "exclude": [
    "node_modules/"
  ]
}
Download .txt
gitextract__9gnz8i2/

├── .babelrc.js
├── .editorconfig
├── .gitignore
├── .np-config.json
├── .prettierrc
├── .travis.yml
├── .yarnrc.yml
├── HISTORY.md
├── LICENSE.md
├── README.md
├── __tests__/
│   ├── any.spec.ts
│   ├── array.spec.ts
│   ├── date.spec.ts
│   ├── deep.spec.ts
│   ├── enum.spec.ts
│   ├── messages.spec.ts
│   ├── number.spec.ts
│   ├── object.spec.ts
│   ├── pattern.spec.ts
│   ├── promise.spec.ts
│   ├── required.spec.ts
│   ├── string.spec.ts
│   ├── unicode.spec.ts
│   ├── url.spec.ts
│   └── validator.spec.ts
├── package.json
├── scripts/
│   ├── runTest.ts
│   └── test.sh
├── src/
│   ├── index.ts
│   ├── interface.ts
│   ├── messages.ts
│   ├── rule/
│   │   ├── enum.ts
│   │   ├── index.ts
│   │   ├── pattern.ts
│   │   ├── range.ts
│   │   ├── required.ts
│   │   ├── type.ts
│   │   ├── url.ts
│   │   └── whitespace.ts
│   ├── util.ts
│   └── validator/
│       ├── any.ts
│       ├── array.ts
│       ├── boolean.ts
│       ├── date.ts
│       ├── enum.ts
│       ├── float.ts
│       ├── index.ts
│       ├── integer.ts
│       ├── method.ts
│       ├── number.ts
│       ├── object.ts
│       ├── pattern.ts
│       ├── regexp.ts
│       ├── required.ts
│       ├── string.ts
│       └── type.ts
├── tests/
│   └── url.ts
└── tsconfig.json
Download .txt
SYMBOL INDEX (122 symbols across 10 files)

FILE: __tests__/messages.spec.ts
  method required (line 6) | required(f) {
  method required (line 36) | required(f) {

FILE: __tests__/promise.spec.ts
  method asyncValidator (line 8) | asyncValidator(rule, value) {
  method asyncValidator (line 13) | asyncValidator(rule, value) {
  method asyncValidator (line 20) | asyncValidator(rule, value) {
  method asyncValidator (line 43) | asyncValidator(rule, value) {
  method asyncValidator (line 48) | asyncValidator(rule, value) {
  method asyncValidator (line 55) | asyncValidator(rule, value) {
  method asyncValidator (line 81) | asyncValidator(rule, value) {
  method asyncValidator (line 86) | asyncValidator(rule, value) {
  method asyncValidator (line 94) | asyncValidator(rule, value) {
  method asyncValidator (line 101) | asyncValidator(rule, value) {
  method asyncValidator (line 106) | asyncValidator(rule, value) {
  method asyncValidator (line 139) | asyncValidator(rule, value) {
  method asyncValidator (line 147) | asyncValidator(rule, value) {
  method asyncValidator (line 154) | asyncValidator(rule, value) {
  method asyncValidator (line 159) | asyncValidator(rule, value) {
  method asyncValidator (line 210) | asyncValidator(rule, value) {

FILE: __tests__/validator.spec.ts
  method validator (line 8) | validator(rule, value, callback) {
  method validator (line 13) | validator(rule, value, callback) {
  method validator (line 20) | validator(rule, value, callback) {
  method validator (line 27) | validator() {
  method validator (line 32) | validator() {
  method validator (line 37) | validator() {
  method validator (line 43) | validator() {
  method validator (line 49) | validator() {
  method validator (line 77) | validator(rule, value, callback) {
  method validator (line 82) | validator(rule, value, callback) {
  method validator (line 89) | validator(rule, value, callback) {
  method validator (line 115) | validator(rule, value, callback) {
  method validator (line 120) | validator(rule, value, callback) {
  method validator (line 128) | validator(rule, value, callback) {
  method validator (line 135) | validator(rule, value, callback) {
  method validator (line 140) | validator(rule, value, callback) {
  method validator (line 168) | validator(rule, value, callback) {
  method validator (line 173) | validator(rule, value, callback) {
  method validator (line 181) | validator(rule, value, callback) {
  method validator (line 188) | validator(rule, value, callback) {
  method validator (line 193) | validator(rule, value, callback) {
  method validator (line 224) | validator(rule, value, callback) {
  method validator (line 229) | validator(rule, value, callback) {
  method validator (line 236) | validator(rule, value, callback) {
  method validator (line 243) | validator() {
  method validator (line 248) | validator() {
  method validator (line 253) | validator() {
  method validator (line 259) | validator() {
  method validator (line 313) | validator(rule, value, callback) {
  method validator (line 318) | validator(rule, value, callback) {
  method validator (line 325) | validator(rule, value, callback) {
  method validator (line 352) | validator(rule, value, callback) {
  method validator (line 357) | validator(rule, value, callback) {
  method validator (line 365) | validator(rule, value, callback) {
  method validator (line 372) | validator(rule, value, callback) {
  method validator (line 377) | validator(rule, value, callback) {
  method validator (line 406) | validator(rule, value, callback) {
  method validator (line 411) | validator(rule, value, callback) {
  method validator (line 419) | validator(rule, value, callback) {
  method validator (line 426) | validator(rule, value, callback) {
  method validator (line 431) | validator(rule, value, callback) {
  method validator (line 478) | validator(rule, value, callback) {

FILE: src/index.ts
  class Schema (line 36) | class Schema {
    method constructor (line 57) | constructor(descriptor: Rules) {
    method define (line 61) | define(rules: Rules) {
    method messages (line 76) | messages(messages?: ValidateMessages) {
    method validate (line 91) | validate(source_: Values, o: any = {}, oc: any = () => {}): Promise<Va...
    method getType (line 326) | getType(rule: InternalRuleItem) {
    method getValidationMethod (line 340) | getValidationMethod(rule: InternalRuleItem) {

FILE: src/interface.ts
  type RuleType (line 3) | type RuleType =
  type ValidateOption (line 21) | interface ValidateOption {
  type SyncErrorType (line 42) | type SyncErrorType = Error | string;
  type SyncValidateResult (line 43) | type SyncValidateResult = boolean | SyncErrorType | SyncErrorType[];
  type ValidateResult (line 44) | type ValidateResult = void | Promise<void> | SyncValidateResult;
  type RuleItem (line 46) | interface RuleItem {
  type Rule (line 76) | type Rule = RuleItem | RuleItem[];
  type Rules (line 78) | type Rules = Record<string, Rule>;
  type ExecuteRule (line 92) | type ExecuteRule = (
  type ExecuteValidator (line 111) | type ExecuteValidator = (
  type ValidateMessage (line 120) | type ValidateMessage<T extends any[] = unknown[]> =
  type FullField (line 123) | type FullField = string | undefined;
  type EnumString (line 124) | type EnumString = string | undefined;
  type Pattern (line 125) | type Pattern = string | RegExp | undefined;
  type Range (line 126) | type Range = number | undefined;
  type Type (line 127) | type Type = string | undefined;
  type ValidateMessages (line 129) | interface ValidateMessages {
  type InternalValidateMessages (line 177) | interface InternalValidateMessages extends ValidateMessages {
  type Value (line 182) | type Value = any;
  type Values (line 183) | type Values = Record<string, Value>;
  type ValidateError (line 186) | interface ValidateError {
  type ValidateFieldsError (line 192) | type ValidateFieldsError = Record<string, ValidateError[]>;
  type ValidateCallback (line 194) | type ValidateCallback = (
  type RuleValuePackage (line 199) | interface RuleValuePackage {
  type InternalRuleItem (line 206) | interface InternalRuleItem extends Omit<RuleItem, 'validator'> {

FILE: src/messages.ts
  function newMessages (line 3) | function newMessages(): InternalValidateMessages {

FILE: src/rule/enum.ts
  constant ENUM (line 4) | const ENUM = 'enum' as const;

FILE: src/rule/type.ts
  method integer (line 18) | integer(value: Value) {
  method float (line 21) | float(value: Value) {
  method array (line 24) | array(value: Value) {
  method regexp (line 27) | regexp(value: Value) {
  method date (line 37) | date(value: Value) {
  method number (line 45) | number(value: Value) {
  method object (line 51) | object(value: Value) {
  method method (line 54) | method(value: Value) {
  method email (line 57) | email(value: Value) {
  method url (line 64) | url(value: Value) {
  method hex (line 71) | hex(value: Value) {

FILE: src/util.ts
  function convertFieldsError (line 41) | function convertFieldsError(
  function format (line 54) | function format(
  function isNativeStringType (line 92) | function isNativeStringType(type: string) {
  function isEmptyValue (line 103) | function isEmptyValue(value: Value, type?: string) {
  function isEmptyObject (line 116) | function isEmptyObject(obj: object) {
  function asyncParallelArray (line 120) | function asyncParallelArray(
  function asyncSerialArray (line 142) | function asyncSerialArray(
  function flattenObjArr (line 167) | function flattenObjArr(objArr: Record<string, RuleValuePackage[]>) {
  class AsyncValidationError (line 175) | class AsyncValidationError extends Error {
    method constructor (line 179) | constructor(
  type ValidateFunc (line 189) | type ValidateFunc = (
  function asyncMap (line 194) | function asyncMap(
  function isErrorObj (line 254) | function isErrorObj(
  function getValue (line 260) | function getValue(value: Values, path: string[]) {
  function complementError (line 271) | function complementError(rule: InternalRuleItem, source: Values) {
  function deepMerge (line 292) | function deepMerge<T extends object>(target: T, source: Partial<T>): T {

FILE: src/validator/enum.ts
  constant ENUM (line 5) | const ENUM = 'enum' as const;
Condensed preview — 58 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (126K chars).
[
  {
    "path": ".babelrc.js",
    "chars": 332,
    "preview": "console.log('Load babel config');\n\nmodule.exports = api => {\n  return {\n    presets: [\n      [\n        '@babel/preset-en"
  },
  {
    "path": ".editorconfig",
    "chars": 192,
    "preview": "# top-most EditorConfig file\nroot = true\n\n# Unix-style newlines with a newline ending every file\n[*.{js,css}]\nend_of_lin"
  },
  {
    "path": ".gitignore",
    "chars": 690,
    "preview": "# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.\n\n# dependencies\nnode_modules\n/.yar"
  },
  {
    "path": ".np-config.json",
    "chars": 21,
    "preview": "{\n    \"yarn\": false\n}"
  },
  {
    "path": ".prettierrc",
    "chars": 51,
    "preview": "{\n  \"singleQuote\": true,\n  \"trailingComma\": \"all\"\n}"
  },
  {
    "path": ".travis.yml",
    "chars": 164,
    "preview": "language: node_js\n\nnotifications:\n    email:\n        - yiminghe@gmail.com\n\nnode_js:\n  - 16.13.1\n\nbefore_install:\n  - cor"
  },
  {
    "path": ".yarnrc.yml",
    "chars": 24,
    "preview": "nodeLinker: node-modules"
  },
  {
    "path": "HISTORY.md",
    "chars": 1615,
    "preview": "# History\n----\n\n## 4.0.0 / 2021-08-11\n\n- full ts support\n- support return transformed value when pass validation(promise"
  },
  {
    "path": "LICENSE.md",
    "chars": 1083,
    "preview": "The MIT License (MIT)\n\nCopyright (c) 2014-present yiminghe\n\nPermission is hereby granted, free of charge, to any person "
  },
  {
    "path": "README.md",
    "chars": 14893,
    "preview": "# async-validator\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][co"
  },
  {
    "path": "__tests__/any.spec.ts",
    "chars": 1045,
    "preview": "import Schema from '../src';\n\nconst testNoErrorsFor = value => done => {\n  new Schema({\n    v: {\n      type: 'any',\n    "
  },
  {
    "path": "__tests__/array.spec.ts",
    "chars": 3824,
    "preview": "import Schema from '../src';\n\ndescribe('array', () => {\n  it('works for type', done => {\n    new Schema({\n      v: {\n   "
  },
  {
    "path": "__tests__/date.spec.ts",
    "chars": 1344,
    "preview": "import Schema from '../src';\n\ndescribe('date', () => {\n  it('required works for undefined', done => {\n    new Schema({\n "
  },
  {
    "path": "__tests__/deep.spec.ts",
    "chars": 6006,
    "preview": "import Schema, { Rules } from '../src';\n\ndescribe('deep', () => {\n  it('deep array specific validation', done => {\n    n"
  },
  {
    "path": "__tests__/enum.spec.ts",
    "chars": 396,
    "preview": "import Schema from '../src';\n\ndescribe('enum', () => {\n  it('run validation on `false`', done => {\n    new Schema({\n    "
  },
  {
    "path": "__tests__/messages.spec.ts",
    "chars": 2908,
    "preview": "import Schema, { ValidateMessages } from '../src';\n\ndescribe('messages', () => {\n  it('can call messages', done => {\n   "
  },
  {
    "path": "__tests__/number.spec.ts",
    "chars": 2024,
    "preview": "import Schema from '../src';\n\ndescribe('number', () => {\n  it('works', done => {\n    new Schema({\n      v: {\n        typ"
  },
  {
    "path": "__tests__/object.spec.ts",
    "chars": 456,
    "preview": "import Schema from '../src';\n\ndescribe('object', () => {\n  it('works for the required object with fields in case of empt"
  },
  {
    "path": "__tests__/pattern.spec.ts",
    "chars": 2546,
    "preview": "import Schema from '../src';\n\ndescribe('pattern', () => {\n  it('works for non-required empty string', done => {\n    new "
  },
  {
    "path": "__tests__/promise.spec.ts",
    "chars": 5501,
    "preview": "import Schema from '../src';\n\ndescribe('asyncValidator', () => {\n  it('works', done => {\n    new Schema({\n      v: [\n   "
  },
  {
    "path": "__tests__/required.spec.ts",
    "chars": 3536,
    "preview": "import Schema from '../src';\nconst required = true;\n\ndescribe('required', () => {\n  it('works for array required=true', "
  },
  {
    "path": "__tests__/string.spec.ts",
    "chars": 2334,
    "preview": "import Schema from '../src';\n\ndescribe('string', () => {\n  it('works for none require', done => {\n    let data = {\n     "
  },
  {
    "path": "__tests__/unicode.spec.ts",
    "chars": 888,
    "preview": "import Schema from '../src';\n\ndescribe('unicode', () => {\n  it('works for unicode U+0000 to U+FFFF ', done => {\n    new "
  },
  {
    "path": "__tests__/url.spec.ts",
    "chars": 2902,
    "preview": "import Schema from '../src';\n\ndescribe('url', () => {\n  it('works for empty string', done => {\n    new Schema({\n      v:"
  },
  {
    "path": "__tests__/validator.spec.ts",
    "chars": 11183,
    "preview": "import Schema from '../src';\n\ndescribe('validator', () => {\n  it('works', done => {\n    new Schema({\n      v: [\n        "
  },
  {
    "path": "package.json",
    "chars": 2308,
    "preview": "{\n  \"name\": \"async-validator\",\n  \"version\": \"4.2.5\",\n  \"description\": \"validate form asynchronous\",\n  \"typings\": \"typing"
  },
  {
    "path": "scripts/runTest.ts",
    "chars": 577,
    "preview": "import Schema, {\n  ValidateCallback,\n  ValidateFieldsError,\n  Values,\n} from '../src/index';\n\nconst callback: ValidateCa"
  },
  {
    "path": "scripts/test.sh",
    "chars": 65,
    "preview": "NODE_ENV=test npx babel-node -x \".ts,.js,.jsx\" scripts/runTest.ts"
  },
  {
    "path": "src/index.ts",
    "chars": 10370,
    "preview": "import {\n  format,\n  complementError,\n  asyncMap,\n  warning,\n  deepMerge,\n  convertFieldsError,\n} from './util';\nimport "
  },
  {
    "path": "src/interface.ts",
    "chars": 6243,
    "preview": "// >>>>> Rule\n// Modified from https://github.com/yiminghe/async-validator/blob/0d51d60086a127b21db76f44dff28ae18c165c47"
  },
  {
    "path": "src/messages.ts",
    "chars": 1797,
    "preview": "import { InternalValidateMessages } from './interface';\n\nexport function newMessages(): InternalValidateMessages {\n  ret"
  },
  {
    "path": "src/rule/enum.ts",
    "chars": 424,
    "preview": "import { ExecuteRule } from '../interface';\nimport { format } from '../util';\n\nconst ENUM = 'enum' as const;\n\nconst enum"
  },
  {
    "path": "src/rule/index.ts",
    "chars": 287,
    "preview": "import required from './required';\nimport whitespace from './whitespace';\nimport type from './type';\nimport range from '"
  },
  {
    "path": "src/rule/pattern.ts",
    "chars": 1061,
    "preview": "import { ExecuteRule } from '../interface';\nimport { format } from '../util';\n\nconst pattern: ExecuteRule = (rule, value"
  },
  {
    "path": "src/rule/range.ts",
    "chars": 1608,
    "preview": "import { ExecuteRule } from '../interface';\nimport { format } from '../util';\n\nconst range: ExecuteRule = (rule, value, "
  },
  {
    "path": "src/rule/required.ts",
    "chars": 397,
    "preview": "import { ExecuteRule } from '../interface';\nimport { format, isEmptyValue } from '../util';\n\nconst required: ExecuteRule"
  },
  {
    "path": "src/rule/type.ts",
    "chars": 3064,
    "preview": "import { ExecuteRule, Value } from '../interface';\nimport { format } from '../util';\nimport required from './required';\n"
  },
  {
    "path": "src/rule/url.ts",
    "chars": 3053,
    "preview": "// https://github.com/kevva/url-regex/blob/master/index.js\nlet urlReg: RegExp;\n\nexport default () => {\n  if (urlReg) {\n "
  },
  {
    "path": "src/rule/whitespace.ts",
    "chars": 686,
    "preview": "import { ExecuteRule } from '../interface';\nimport { format } from '../util';\n\n/**\n *  Rule for validating whitespace.\n "
  },
  {
    "path": "src/util.ts",
    "chars": 7305,
    "preview": "/* eslint no-console:0 */\n\nimport {\n  ValidateError,\n  ValidateOption,\n  RuleValuePackage,\n  InternalRuleItem,\n  SyncErr"
  },
  {
    "path": "src/validator/any.ts",
    "chars": 525,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/array.ts",
    "chars": 692,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule/index';\n\nconst array: ExecuteValidator = (ru"
  },
  {
    "path": "src/validator/boolean.ts",
    "chars": 626,
    "preview": "import { isEmptyValue } from '../util';\nimport rules from '../rule';\nimport { ExecuteValidator } from '../interface';\n\nc"
  },
  {
    "path": "src/validator/date.ts",
    "chars": 995,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/enum.ts",
    "chars": 677,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/float.ts",
    "chars": 683,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/index.ts",
    "chars": 675,
    "preview": "import string from './string';\nimport method from './method';\nimport number from './number';\nimport boolean from './bool"
  },
  {
    "path": "src/validator/integer.ts",
    "chars": 683,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/method.ts",
    "chars": 624,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/number.ts",
    "chars": 736,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/object.ts",
    "chars": 624,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/pattern.ts",
    "chars": 650,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/regexp.ts",
    "chars": 625,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/required.ts",
    "chars": 364,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\n\nconst required: ExecuteValidator = (rule,"
  },
  {
    "path": "src/validator/string.ts",
    "chars": 881,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "src/validator/type.ts",
    "chars": 681,
    "preview": "import { ExecuteValidator } from '../interface';\nimport rules from '../rule';\nimport { isEmptyValue } from '../util';\n\nc"
  },
  {
    "path": "tests/url.ts",
    "chars": 768,
    "preview": "import AsyncValidator from '../src';\n\nconst validator = new AsyncValidator({\n  v: {\n    type: 'url',\n  },\n});\n\nfor (var "
  },
  {
    "path": "tsconfig.json",
    "chars": 603,
    "preview": "{\n  \"compilerOptions\": {\n    \"outDir\": \"lib\",\n    \"module\": \"commonjs\",\n    \"target\": \"ES2020\",\n    \"lib\": [\n      \"es20"
  }
]

About this extraction

This page contains the full source code of the yiminghe/async-validator GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 58 files (114.6 KB), approximately 30.5k tokens, and a symbol index with 122 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!