Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support len validator #188

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const validators = {
notEmpty(str) {
return /[^\s]/.test(str);
},
len(str, min, max) {
return Validator.isLength(str, min, max);
}
};

/**
Expand Down
20 changes: 18 additions & 2 deletions test/unit/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('validator', () => {
isNumeric: false,
notIn: [ [ 'Yhorm', 'Gwyn' ] ],
notContains: 'closePease',
len: [ 2, 9 ],
}
},
meta: {
Expand Down Expand Up @@ -108,13 +109,13 @@ describe('validator', () => {
await assert.rejects(async () => {
await User.create({
email: 'sss@e.cn',
nickname: 's',
nickname: 's21',
});
}, /Validation is on email failed/);

await User.create({
email: 'sss@e.com',
nickname: 's',
nickname: 's321',
});
});

Expand Down Expand Up @@ -307,6 +308,20 @@ describe('validator', () => {
assert(user);
});

it('len', async () => {
await assert.rejects(async () => {
await User.create({
email: 'a@e.com',
nickname: 's',
});
}, /Validation len on nickname failed/);
const user = await User.create({
email: 'a1@e.com',
nickname: 'sss1',
});
assert(user);
});

it('should be a valid validator', async () => {
await assert.rejects(async () => {
await User.create({
Expand All @@ -319,6 +334,7 @@ describe('validator', () => {
});
}, /Invalid validator function: hhh/);
});

});

describe('create', () => {
Expand Down