Skip to content

Commit 654a67c

Browse files
added _match schema_ validation (#8)
1 parent 5d0fca3 commit 654a67c

File tree

6 files changed

+164
-6
lines changed

6 files changed

+164
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
## 0.0.5
2+
- :rocket: added _match schema_ validation
3+
14
## 0.0.4
25
- :rocket: added _have property_ validation

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Lib supports following validations
1616
* have members - validation if array/object have exact members
1717
* include members - validation if array/object includes members
1818
* have property - have property validation
19+
* match schema - match [ajv](https://www.npmjs.com/package/ajv) schema
1920

2021
All validations can be negated adding _not_ word.
2122

package-lock.json

Lines changed: 91 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/validation",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Lib that transform plain english definition to validation functions",
55
"main": "index.js",
66
"scripts": {
@@ -28,10 +28,11 @@
2828
"devDependencies": {
2929
"@types/chai": "^4.3.4",
3030
"jest": "^29.5.0",
31-
"typescript": "^5.0.2",
32-
"ts-jest": "^29.0.5"
31+
"ts-jest": "^29.0.5",
32+
"typescript": "^5.0.2"
3333
},
3434
"dependencies": {
35+
"ajv": "^8.12.0",
3536
"chai": "^4.3.7"
3637
}
3738
}

src/verify.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, Assertion } from 'chai';
2+
import Ajv from 'ajv'
23

34
Assertion.addMethod('notStrictEqual', function (ER) {
45
const obj = this._obj;
@@ -12,6 +13,25 @@ Assertion.addMethod('notStrictEqual', function (ER) {
1213
);
1314
});
1415

16+
Assertion.addMethod('matchSchema', function (schema) {
17+
const obj = this._obj;
18+
const ajv = new Ajv();
19+
const validate = ajv.compile(schema);
20+
const isValid = validate(obj);
21+
const messages = validate.errors ? validate.errors?.map(err => err.message) : [];
22+
const errors = [
23+
'expected #{this} to match schema #{exp}',
24+
...messages
25+
].join('\n');
26+
this.assert(
27+
isValid,
28+
errors,
29+
'expected #{this} to not match schema #{exp}',
30+
schema,
31+
obj
32+
);
33+
});
34+
1535
export const validations = {
1636
EQUAL: 'equal',
1737
DEEPLY_EQUAL: 'deeply equal',
@@ -25,7 +45,8 @@ export const validations = {
2545
LESS: 'less than',
2646
HAVE_TYPE: 'have type',
2747
INCLUDE_MEMBERS: 'include member',
28-
HAVE_PROPERTY: 'have property'
48+
HAVE_PROPERTY: 'have property',
49+
MATCH_SCHEMA: 'match schema'
2950
};
3051

3152
const isClause = '(?:is |do |does |to )?';
@@ -59,6 +80,7 @@ const validationFns = {
5980
[validations.HAVE_TYPE]: (expectClause: any, ER: string) => expectClause.a(ER),
6081
[validations.INCLUDE_MEMBERS]: (expectClause: any, ER: string) => expectClause.include.members(ER),
6182
[validations.HAVE_PROPERTY]: (expectClause: any, ER: string) => expectClause.have.property(ER),
83+
[validations.MATCH_SCHEMA]: (expectClause: any, ER: string) => expectClause.matchSchema(ER),
6284
};
6385

6486
/**

test/validationTransformer.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,48 @@ const tests: Array<TestParams> = [
323323
negativeArgs: [{ prop: 42 }, 'prop'],
324324
expectedError: 'expected { prop: 42 } to not have property \'prop\'',
325325
},
326+
{
327+
testName: 'to match schema',
328+
validation: 'to match schema',
329+
positiveArgs: [{ prop: 42 }, {
330+
type: 'object',
331+
properties: {
332+
prop: {type: 'integer'},
333+
},
334+
required: ['prop'],
335+
additionalProperties: false,
336+
}],
337+
negativeArgs: [{ prop2: 42 }, {
338+
type: 'object',
339+
properties: {
340+
prop: {type: 'integer'},
341+
},
342+
required: ['prop'],
343+
additionalProperties: false,
344+
}],
345+
expectedError: 'expected { prop2: 42 } to match schema { type: \'object\', …(3) }\nmust have required property \'prop\'',
346+
},
347+
{
348+
testName: 'not to match schema',
349+
validation: 'not to match schema',
350+
positiveArgs: [{ prop: 42 }, {
351+
type: 'object',
352+
properties: {
353+
prop: {type: 'string'},
354+
},
355+
required: ['prop'],
356+
additionalProperties: false,
357+
}],
358+
negativeArgs: [{ prop: 42 }, {
359+
type: 'object',
360+
properties: {
361+
prop: {type: 'integer'},
362+
},
363+
required: ['prop'],
364+
additionalProperties: false,
365+
}],
366+
expectedError: 'expected { prop: 42 } to not match schema { type: \'object\', …(3) }',
367+
},
326368
];
327369

328370
test.each(tests)('$testName', ({ validation, positiveArgs, negativeArgs, expectedError }: TestParams) => {

0 commit comments

Comments
 (0)