Skip to content

Commit

Permalink
Merge branch 'feature-improve-validation-reporting-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
shellyln committed Feb 20, 2020
2 parents dda2d51 + ae00f59 commit 2c3aba5
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 58 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

## v0.1.7
### Breaking changes
* Change the `dataPath` format for validation errors.
* Path separator after `type` is changed from `.` to `:`.
* before changed: `File.acl.(0:repeated).ACL.target`
* after changed: `File:acl.(0:repeated).ACL:target`

### Changes
* `[FIX]` Fix validation reporting. (incorrect parent name)

---
4 changes: 2 additions & 2 deletions src/_spec/compiler-2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ describe("compiler-2", function() {
expect(ctx1.errors).toEqual([{
code: 'InvalidDefinition',
message: '"entries" of "Folder" type definition is invalid.',
dataPath: 'Folder.entries.(0:repeated).Entry',
dataPath: 'Folder:entries.(0:repeated).Entry',
constraints: {}
}]);
expect(validate<any>(v, ty, {schema})).toEqual({value: v});
Expand Down Expand Up @@ -880,7 +880,7 @@ describe("compiler-2", function() {
expect(ctx1.errors).toEqual([{
code: 'InvalidDefinition',
message: '"entries" of "Folder" type definition is invalid.',
dataPath: 'Folder.entries.(0:sequence).Entry',
dataPath: 'Folder:entries.(0:sequence).Entry',
constraints: {}
}]);
expect(validate<any>(v, ty, {schema})).toEqual({value: v});
Expand Down
2 changes: 1 addition & 1 deletion src/_spec/compiler-3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ describe("compiler-3", function() {
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched',
message: '"D" of "C" should be type "number".',
dataPath: 'C.D',
dataPath: 'C:D',
constraints: {},
value: '',
}]);
Expand Down
32 changes: 16 additions & 16 deletions src/_spec/compiler-7.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'Required',
message: 'A.a1:required: a1 A',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {},
}]);
});
Expand All @@ -578,7 +578,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched',
message: 'A.a1:typeUnmatched: a1 A string',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {},
value: 1,
}]);
Expand Down Expand Up @@ -607,7 +607,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched',
message: 'A.a1:typeUnmatched: a1 A string',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {},
}]);
}
Expand Down Expand Up @@ -635,7 +635,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched',
message: 'A.a1:typeUnmatched: a1 A (repeated string)',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {},
value: '1',
}]);
Expand Down Expand Up @@ -664,7 +664,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched',
message: '"repeated item of a1" of "A" should be type "string".', // TODO:
dataPath: 'A.a1.(0:repeated)',
dataPath: 'A:a1.(0:repeated)',
constraints: {},
value: 1,
}]);
Expand Down Expand Up @@ -693,7 +693,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched',
message: 'A.a1:typeUnmatched: a1 A (sequence)',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {},
value: '1',
}]);
Expand Down Expand Up @@ -722,7 +722,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched',
message: '"sequence item of a1" of "A" should be type "string".', // TODO:
dataPath: 'A.a1.(0:sequence)',
dataPath: 'A:a1.(0:sequence)',
constraints: {},
value: 1,
}]);
Expand Down Expand Up @@ -753,7 +753,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'ValueRangeUnmatched',
message: 'A.a1:valueRangeUnmatched: a1 A 3 5',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {minValue: 3, maxValue: 5},
value: 1,
}]);
Expand Down Expand Up @@ -784,7 +784,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'ValueLengthUnmatched',
message: 'A.a1:valueLengthUnmatched: a1 A 3 5',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {minLength: 3, maxLength: 5},
value: '1',
}]);
Expand Down Expand Up @@ -816,7 +816,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'ValuePatternUnmatched',
message: 'A.a1:valuePatternUnmatched: a1 A /^[0-9]+$/',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {pattern: '/^[0-9]+$/'},
value: 'A',
}]);
Expand Down Expand Up @@ -849,7 +849,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'ValuePatternUnmatched',
message: 'A.a1:valuePatternUnmatched: a1 A /^[0-9]+$/gi',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {pattern: '/^[0-9]+$/gi'},
value: 'A',
}]);
Expand Down Expand Up @@ -879,7 +879,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'ValueUnmatched',
message: 'A.a1:valueUnmatched: a1 A 5',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {},
value: 4,
}]);
Expand Down Expand Up @@ -908,7 +908,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched', // TODO: expect SequenceUnmatched?
message: '"sequence item of a1" of "A" should be type "string".', // TODO: custom error message is ignored
dataPath: 'A.a1.(0:sequence)',
dataPath: 'A:a1.(0:sequence)',
constraints: {min: 3, max: 5},
}]);
}
Expand Down Expand Up @@ -936,7 +936,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'RepeatQtyUnmatched',
message: '"sequence item of a1" of "A" should repeat 3..5 times.', // TODO: custom error message is ignored
dataPath: 'A.a1.(2:sequence)',
dataPath: 'A:a1.(2:sequence)',
constraints: {min: 3, max: 5},
}]);
}
Expand Down Expand Up @@ -964,7 +964,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'SequenceUnmatched',
message: 'A.a1:sequenceUnmatched: a1 A',
dataPath: 'A.a1',
dataPath: 'A:a1',
constraints: {},
}]);
}
Expand Down Expand Up @@ -992,7 +992,7 @@ describe("compiler-7", function() {
expect(ctx.errors).toEqual([{
code: 'RepeatQtyUnmatched',
message: '"sequence item of a1" of "A" should repeat 0..1 times.', // TODO: custom error message is ignored
dataPath: 'A.a1.(2:sequence)',
dataPath: 'A:a1.(2:sequence)',
constraints: {},
}]);
}
Expand Down
60 changes: 60 additions & 0 deletions src/_spec/fixes-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,4 +745,64 @@ describe("fix-1", function() {
err.message.includes(
'interfaceKey: Unexpected token has appeared. Expect "]".\n'));
});
it("fix-improve-validation-message-1", function() {
const schema = compile(`
interface ACL {
target: string;
value: string;
}
/** Entry base */
interface EntryBase {
/** Entry name */
name: string;
/** ACL infos */
acl: ACL[];
}
/** File entry */
interface File extends EntryBase {
/** Entry type */
type: 'file';
}
/** Folder entry */
interface Folder extends EntryBase {
/** Entry type */
type: 'folder';
/** Child entries */
entries: Entry[];
}
/** Entry (union type) */
type Entry = File | Folder;
type Foo = string[]|number;
`);
{
const ctx: Partial<ValidationContext> = {checkAll: true};
const z = validate<any>({type: 'file', name: '', acl: [{}]}, getType(schema, 'File'), ctx);
expect(ctx.errors).toEqual([{
code: 'Required',
message: '"target" of "ACL" is required.',
dataPath: 'File:acl.(0:repeated).ACL:target',
constraints: {},
}, {
code: 'Required',
message: '"value" of "ACL" is required.',
dataPath: 'File:acl.(0:repeated).ACL:value',
constraints: {},
}] as any);
}
{
const ctx: Partial<ValidationContext> = {checkAll: true};
const z = validate<any>({type: 'file', name: '', acl: [1]}, getType(schema, 'File'), ctx);
expect(ctx.errors).toEqual([{
code: 'TypeUnmatched',
message: '"acl" of "File" should be type "ACL".',
dataPath: 'File:acl.(0:repeated).ACL',
constraints: {},
value: 1,
}] as any);
}
});
});
Loading

0 comments on commit 2c3aba5

Please sign in to comment.