Skip to content

Commit 68ca9dd

Browse files
committed
Merge branch 'bauglir-fix-data-switch'
2 parents f36537c + c15d1ef commit 68ca9dd

4 files changed

Lines changed: 51 additions & 3 deletions

File tree

src/commands/options.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,13 @@ function parameter(str: string): string {
9393

9494
export function getOptions(argv: ParsedArgs): Options & {code: CodeOptions} {
9595
const options: {[K in string]: any} = {code: {}}
96-
for (let opt in ajvOptions) {
97-
if (opt === "data") opt = "$data"
96+
for (const opt in ajvOptions) {
9897
const value = argv[toDashCase(opt)] ?? argv[opt]
9998
if (value === undefined) continue
10099
if (opt.startsWith(CODE)) {
101100
options.code[opt.slice(CODE.length)] = value
102101
} else {
103-
options[opt] = value
102+
options[opt === "data" ? "$data" : opt] = value
104103
}
105104
}
106105
return options as Options & {code: CodeOptions}
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)