Skip to content

Commit 53e5033

Browse files
authored
Merge pull request #200 from json-schema-tools/fix/simple-type-validation
fix: fix for simple type validation (boolean, string, number) + added tests
2 parents 0816067 + a91146c commit 53e5033

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

src/index.test.ts

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { ObjectValidationError } from "./base-validators/object";
44
import { NumberValidationError } from "./base-validators/number";
55

66
describe("validator", () => {
7-
it("is a function", () => { expect(typeof validator).toBe("function"); });
7+
it("is a function", () => {
8+
expect(typeof validator).toBe("function");
9+
});
810

911
it("can handle mixed schema", () => {
1012
const testSchema = {
@@ -13,14 +15,14 @@ describe("validator", () => {
1315
foo: { type: "string", enum: ["abc", "xyz"] },
1416
bar: {
1517
type: "array",
16-
items: { type: "number" }
17-
}
18-
}
18+
items: { type: "number" },
19+
},
20+
},
1921
} as JSONSchema;
2022

2123
const result = validator(testSchema, {
2224
foo: "abc",
23-
bar: [123]
25+
bar: [123],
2426
});
2527

2628
expect(result).toBe(true);
@@ -33,14 +35,14 @@ describe("validator", () => {
3335
foo: { type: "string", enum: ["abc", "xyz"] },
3436
bar: {
3537
type: "array",
36-
items: { type: "number" }
37-
}
38-
}
38+
items: { type: "number" },
39+
},
40+
},
3941
} as JSONSchema;
4042

4143
const result = validator(testSchema, {
4244
foo: "wrong",
43-
bar: [123]
45+
bar: [123],
4446
});
4547

4648
expect(result).toBeInstanceOf(ValidationErrors);
@@ -54,14 +56,14 @@ describe("validator", () => {
5456
foo: { type: "string", enum: ["abc", "xyz"] },
5557
bar: {
5658
type: "array",
57-
items: { type: "number" }
58-
}
59-
}
59+
items: { type: "number" },
60+
},
61+
},
6062
} as JSONSchema;
6163

6264
const result = validator(testSchema, {
6365
foo: "wrong",
64-
bar: ["alsoWrong"]
66+
bar: ["alsoWrong"],
6567
});
6668

6769
expect(result).toBeInstanceOf(ValidationErrors);
@@ -74,12 +76,12 @@ describe("validator", () => {
7476
required: ["foo"],
7577
properties: {
7678
foo: { type: "string" },
77-
bar: true
78-
}
79+
bar: true,
80+
},
7981
} as JSONSchema;
8082

8183
const result = validator(testSchema, {
82-
bar: "anything"
84+
bar: "anything",
8385
});
8486

8587
expect(result).toBeInstanceOf(ValidationErrors);
@@ -91,12 +93,12 @@ describe("validator", () => {
9193
type: "object",
9294
properties: {
9395
foo: { type: "string" },
94-
bar: true
95-
}
96+
bar: true,
97+
},
9698
} as JSONSchema;
9799

98100
const result = validator(testSchema, {
99-
bar: "anything"
101+
bar: "anything",
100102
});
101103

102104
expect(result).toBe(true);
@@ -109,8 +111,8 @@ describe("validator", () => {
109111
properties: {
110112
foo: { type: "string" },
111113
bar: true,
112-
baz: { type: "number" }
113-
}
114+
baz: { type: "number" },
115+
},
114116
} as JSONSchema;
115117

116118
const result = validator(testSchema, {
@@ -136,8 +138,8 @@ describe("validator", () => {
136138
properties: {
137139
foo: { type: "string" },
138140
bar: true,
139-
baz: { type: "number" }
140-
}
141+
baz: { type: "number" },
142+
},
141143
} as JSONSchema;
142144

143145
const result = validator(testSchema, {
@@ -148,4 +150,34 @@ describe("validator", () => {
148150
expect(result.errors[0]).toBeInstanceOf(NumberValidationError);
149151
expect(result.errors[1]).toBeInstanceOf(ObjectValidationError);
150152
});
153+
describe("handles paths correctly when passed simple types", () => {
154+
it("handles paths correctly when passed a boolean", () => {
155+
const testSchema = {
156+
type: "boolean",
157+
} as JSONSchema;
158+
159+
const result = validator(testSchema, true) as any;
160+
161+
expect(result).toBe(true);
162+
});
163+
164+
it("handles paths correctly when passed a string", () => {
165+
const testSchema = {
166+
type: "string",
167+
} as JSONSchema;
168+
169+
const result = validator(testSchema, "potato") as any;
170+
171+
expect(result).toBe(true);
172+
});
173+
it("handles paths correctly when passed a number", () => {
174+
const testSchema = {
175+
type: "number",
176+
} as JSONSchema;
177+
178+
const result = validator(testSchema, 1) as any;
179+
180+
expect(result).toBe(true);
181+
});
182+
});
151183
});

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ const validator = (schema: JSONSchema, data: any): true | ValidationErrors => {
123123

124124
traverse(schema, (ss, isCycle, path, parent: JSONSchema) => {
125125
const regularPath = schemaPathToRegularPath(path);
126-
const [reffed] = jsonpath.query(data, regularPath);
126+
let reffed = data;
127+
if (data instanceof Object) {
128+
const [r] = jsonpath.query(data, regularPath);
129+
reffed = r;
130+
}
127131

128132
let result: boolean | ValidationError[] = true;
129133
if (reffed === undefined) {

0 commit comments

Comments
 (0)