Skip to content

Commit 8c440a5

Browse files
committed
fix: set $data option in the presence of the --data switch
Fixes #93.
1 parent f36537c commit 8c440a5

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

src/commands/options.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ function parameter(str: string): string {
9494
export function getOptions(argv: ParsedArgs): Options & {code: CodeOptions} {
9595
const options: {[K in string]: any} = {code: {}}
9696
for (let opt in ajvOptions) {
97-
if (opt === "data") opt = "$data"
97+
if (opt === "data") {
98+
opt = "$data"
99+
argv.$data = argv.data
100+
}
98101
const value = argv[toDashCase(opt)] ?? argv[opt]
99102
if (value === undefined) continue
100103
if (opt.startsWith(CODE)) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"larger": 20,
3+
"smaller": 10
4+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "schema_with_data.json",
4+
"description": "schema to test $data references",
5+
"type": "object",
6+
"properties": {
7+
"larger": {
8+
"type": "number",
9+
"minimum": {
10+
"$data": "1/smaller"
11+
}
12+
},
13+
"smaller": {
14+
"type": "number"
15+
}
16+
}
17+
}

test/validate.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,34 @@ describe("validate", function () {
232232
})
233233
})
234234

235+
describe('option "data"', () => {
236+
it("should exit with error when not specified in the presence of `$data` references", (done) => {
237+
cli(
238+
"validate -s test/schema_with_data_reference -d test/data_for_schema_with_data_reference",
239+
(error, stdout, stderr) => {
240+
assert(error instanceof Error)
241+
assert.strictEqual(stdout, "")
242+
assert(stderr.includes("test/schema_with_data_reference is invalid"))
243+
assert(stderr.includes("larger/minimum"))
244+
assert(stderr.includes("should be number"))
245+
done()
246+
}
247+
)
248+
})
249+
250+
it("it should enable `$data` references when specified", (done) => {
251+
cli(
252+
"validate --data -s test/schema_with_data_reference -d test/data_for_schema_with_data_reference",
253+
(error, stdout, stderr) => {
254+
assert.strictEqual(error, null)
255+
assertValid(stdout, 1)
256+
assert.strictEqual(stderr, "")
257+
done()
258+
}
259+
)
260+
})
261+
})
262+
235263
describe("custom keywords", () => {
236264
it("should validate valid data; custom keyword definition in file", (done) => {
237265
cli(

0 commit comments

Comments
 (0)