Skip to content

Commit

Permalink
full ts support
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Aug 11, 2021
1 parent d0e89fb commit d703767
Show file tree
Hide file tree
Showing 27 changed files with 450 additions and 283 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
@@ -1,6 +1,11 @@
# 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
Expand Down
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -300,7 +300,7 @@ 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 re-assigned to the source object to mutate the value of the property in place.
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';
Expand All @@ -316,8 +316,13 @@ const descriptor = {
};
const validator = new Schema(descriptor);
const source = { name: ' user ' };

validator.validate(source)
.then(() => assert.equal(source.name, 'user'));
.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.
Expand Down
10 changes: 0 additions & 10 deletions __tests__/__snapshots__/deep.spec.js.snap

This file was deleted.

2 changes: 1 addition & 1 deletion __tests__/any.spec.js → __tests__/any.spec.ts
@@ -1,4 +1,4 @@
import Schema from '../src/';
import Schema from '../src';

const testNoErrorsFor = value => done => {
new Schema({
Expand Down
15 changes: 13 additions & 2 deletions __tests__/array.spec.js → __tests__/array.spec.ts
@@ -1,4 +1,4 @@
import Schema from '../src/';
import Schema from '../src';

describe('array', () => {
it('works for type', done => {
Expand Down Expand Up @@ -28,8 +28,19 @@ describe('array', () => {
{
v: '',
},
errors => {
(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();
},
Expand Down
2 changes: 1 addition & 1 deletion __tests__/date.spec.js → __tests__/date.spec.ts
@@ -1,4 +1,4 @@
import Schema from '../src/';
import Schema from '../src';

describe('date', () => {
it('required works for undefined', done => {
Expand Down
150 changes: 0 additions & 150 deletions __tests__/deep.spec.js

This file was deleted.

0 comments on commit d703767

Please sign in to comment.