Socket
Socket
Sign inDemoInstall

async-validator

Package Overview
Dependencies
Maintainers
1
Versions
86
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-validator - npm Package Compare versions

Comparing version 3.3.0 to 3.4.0

19

dist-node/index.js

@@ -182,7 +182,2 @@ 'use strict';

});
for (var arg = args[i]; i < len; arg = args[++i]) {
str += " " + arg;
}
return str;

@@ -195,3 +190,3 @@ }

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

@@ -436,3 +431,3 @@

date: function date(value) {
return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear === 'function';
return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear === 'function' && !isNaN(value.getTime());
},

@@ -971,3 +966,3 @@ number: function number(value) {

if (validate) {
if (isEmptyValue(value) && !rule.required) {
if (isEmptyValue(value, 'date') && !rule.required) {
return callback();

@@ -978,9 +973,9 @@ }

if (!isEmptyValue(value)) {
if (!isEmptyValue(value, 'date')) {
var dateObject;
if (typeof value === 'number') {
if (value instanceof Date) {
dateObject = value;
} else {
dateObject = new Date(value);
} else {
dateObject = value;
}

@@ -987,0 +982,0 @@

@@ -51,3 +51,3 @@ // Type definitions for async-validator 3.0.4

options?: ValidateOption;
defaultField?: { type: RuleType }; // 'object' or 'array' containing validation rules
defaultField?: RuleItem; // 'object' or 'array' containing validation rules
transform?: (value: any) => any;

@@ -54,0 +54,0 @@ message?: string;

@@ -178,7 +178,2 @@ function _extends() {

});
for (var arg = args[i]; i < len; arg = args[++i]) {
str += " " + arg;
}
return str;

@@ -191,3 +186,3 @@ }

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

@@ -432,3 +427,3 @@

date: function date(value) {
return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear === 'function';
return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear === 'function' && !isNaN(value.getTime());
},

@@ -967,3 +962,3 @@ number: function number(value) {

if (validate) {
if (isEmptyValue(value) && !rule.required) {
if (isEmptyValue(value, 'date') && !rule.required) {
return callback();

@@ -974,9 +969,9 @@ }

if (!isEmptyValue(value)) {
if (!isEmptyValue(value, 'date')) {
var dateObject;
if (typeof value === 'number') {
if (value instanceof Date) {
dateObject = value;
} else {
dateObject = new Date(value);
} else {
dateObject = value;
}

@@ -983,0 +978,0 @@

{
"name": "async-validator",
"description": "validate form asynchronous",
"version": "3.3.0",
"version": "3.4.0",
"license": "MIT",

@@ -17,5 +17,5 @@ "files": [

],
"homepage": "http://github.com/yiminghe/async-validator",
"homepage": "https://github.com/yiminghe/async-validator",
"bugs": {
"url": "http://github.com/yiminghe/async-validator/issues"
"url": "https://github.com/yiminghe/async-validator/issues"
},

@@ -22,0 +22,0 @@ "repository": {

# async-validator
---
Validate form asynchronous. A variation of https://github.com/freeformsystems/async-validate
[![NPM version][npm-image]][npm-url]

@@ -26,5 +23,7 @@ [![build status][travis-image]][travis-url]

Validate form asynchronous. A variation of https://github.com/freeformsystems/async-validate
## Install
```
```bash
npm i async-validator

@@ -37,7 +36,7 @@ ```

```javascript
import schema from 'async-validator';
var descriptor = {
```js
import Schema from 'async-validator';
const descriptor = {
name: {
type: "string",
type: 'string',
required: true,

@@ -47,17 +46,17 @@ validator: (rule, value) => value === 'muji',

age: {
type: "number",
type: 'number',
asyncValidator: (rule, value) => {
return new Promise((resolve, reject) => {
if (value < 18) {
reject("too young"); // reject with error message
} else {
resolve();
}
});
}
}
return new Promise((resolve, reject) => {
if (value < 18) {
reject('too young'); // reject with error message
} else {
resolve();
}
});
},
},
};
var validator = new schema(descriptor);
validator.validate({name: "muji"}, (errors, fields) => {
if(errors) {
const validator = new Schema(descriptor);
validator.validate({ name: 'muji' }, (errors, fields) => {
if (errors) {
// validation failed, errors is an array of all errors

@@ -72,7 +71,7 @@ // fields is an object keyed by field name with an array of

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

@@ -84,3 +83,3 @@

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

@@ -112,3 +111,3 @@ ```

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

@@ -126,19 +125,18 @@ ```

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

@@ -152,14 +150,16 @@ }

```javascript
var descriptor = {
```js
const descriptor = {
email: [
{type: "string", required: true, pattern: schema.pattern.email},
{validator(rule, value, callback, source, options) {
var 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: '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;
},
},
],
};
```

@@ -211,6 +211,6 @@

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

@@ -229,15 +229,16 @@

```javascript
var descriptor = {
```js
const descriptor = {
address: {
type: "object", required: true,
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"}
}
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}
}
var validator = new schema(descriptor);
name: { type: 'string', required: true },
};
const validator = new Schema(descriptor);
validator.validate({ address: {} }, (errors, fields) => {

@@ -252,15 +253,17 @@ // errors for address.street, address.city, address.zip

```javascript
var descriptor = {
```js
const descriptor = {
address: {
type: "object", required: true, options: {first: true},
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"}
}
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}
}
var validator = new schema(descriptor);
name: { type: 'string', required: true },
};
const validator = new Schema(descriptor);

@@ -275,16 +278,18 @@ validator.validate({ address: {} })

```javascript
var descriptor = {
```js
const descriptor = {
roles: {
type: "array", required: true, len: 3,
type: 'array',
required: true,
len: 3,
fields: {
0: {type: "string", required: true},
1: {type: "string", required: true},
2: {type: "string", required: true}
}
}
}
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.
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.

@@ -296,9 +301,10 @@ #### defaultField

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

@@ -312,17 +318,18 @@

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

@@ -339,4 +346,4 @@

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

@@ -346,9 +353,9 @@

```javascript
{name:{type: "string", required: true, message: "<b>Name is required</b>"}}
```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:
```javascript
{name:{type: "string", required: true, message: () => this.$t( 'name is required' )}}
```js
{ name: { type: 'string', required: true, message: () => this.$t( 'name is required' ) } }
```

@@ -360,9 +367,9 @@

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

@@ -385,9 +392,9 @@ validator.messages(cn);

url: 'xx',
value: value
value: value,
}).then(function(data) {
callback();
}, function(error) {
callback(new Error(error))
callback(new Error(error));
});
}
},
},

@@ -399,6 +406,6 @@

url: 'xx',
value: value
value: value,
});
}
}
},
},
};

@@ -409,3 +416,3 @@ ```

you can custom validate function for specified field:
You can custom validate function for specified field:

@@ -423,3 +430,3 @@ ```js

validator(rule, value, callback) {
return new Error(`'${value} is not equal to "test".'`);
return new Error(`${value} is not equal to 'test'.`);
},

@@ -434,3 +441,3 @@ },

];
}
},
},

@@ -463,5 +470,4 @@ };

```
```bash
npm test
npm run chrome-test
```

@@ -471,7 +477,7 @@

```
```bash
npm run coverage
```
open coverage/ dir
Open coverage/ dir

@@ -478,0 +484,0 @@ ## License

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc