-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Marcisbee/all-of
Adds `allOf` schema rule
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @this {import('src').Context} | ||
* @param {*} value | ||
* @param {*[]} currentSchema | ||
* @param {(string | number)[]} position | ||
*/ | ||
function validateAllOf(value, currentSchema, position) { | ||
let errorDepth = position.length + 1; | ||
let possibleError; | ||
|
||
const passSize = currentSchema.length; | ||
let passedValues = 0; | ||
|
||
for (const key in currentSchema) { | ||
try { | ||
// Run shallow validation | ||
this.validateSchema.call( | ||
{...this, shallow: true}, | ||
value, | ||
position, | ||
currentSchema[key], | ||
); | ||
|
||
passedValues += 1; | ||
} catch (e) { | ||
if (e.position && e.position.length > errorDepth) { | ||
possibleError = key; | ||
errorDepth = e.position.length; | ||
} | ||
} | ||
} | ||
|
||
if (passedValues >= passSize) { | ||
return; | ||
} | ||
|
||
// Guessing the error based on error depth | ||
if (possibleError) { | ||
return possibleError; | ||
} | ||
|
||
this.error("Value doesn't match all of criteria", "allOf", position); | ||
} | ||
|
||
module.exports = validateAllOf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const {test} = require("uvu"); | ||
const assert = require("uvu/assert"); | ||
|
||
const ValidationError = require("../src/diagnostics/validation-error"); | ||
const output = require("../src/output"); | ||
|
||
const userConfig = {}; | ||
|
||
test("allOf with one empty schema", () => { | ||
const schema = { | ||
"allOf": [{}], | ||
}; | ||
const errors = output("1", schema, userConfig); | ||
|
||
assert.equal(errors, []); | ||
}); | ||
|
||
test("allOf with two empty schemas", () => { | ||
const schema = { | ||
"allOf": [{}, {}], | ||
}; | ||
const errors = output("1", schema, userConfig); | ||
|
||
assert.equal(errors, []); | ||
}); | ||
|
||
test("allOf with two schemas, the first is empty - number is valid", () => { | ||
const schema = { | ||
"allOf": [{}, { "type": "number" }], | ||
}; | ||
const errors = output("1", schema, userConfig); | ||
|
||
assert.equal(errors, []); | ||
}); | ||
|
||
test("allOf with two schemas, the first is empty - string is invalid", () => { | ||
const schema = { | ||
"allOf": [{}, { "type": "number" }], | ||
}; | ||
const errors = output("\"foo\"", schema, userConfig); | ||
|
||
assert.equal(errors, [ | ||
new ValidationError("Value doesn't match all of criteria", "allOf", []) | ||
]); | ||
}); | ||
|
||
test("allOf with two schemas, the second is empty - number is valid", () => { | ||
const schema = { | ||
"allOf": [{ "type": "number" }, {}], | ||
}; | ||
const errors = output("1", schema, userConfig); | ||
|
||
assert.equal(errors, []); | ||
}); | ||
|
||
test("allOf with two schemas, the second is empty - string is invalid", () => { | ||
const schema = { | ||
"allOf": [{ "type": "number" }, {}], | ||
}; | ||
const errors = output("\"foo\"", schema, userConfig); | ||
|
||
assert.equal(errors, [ | ||
new ValidationError("Value doesn't match all of criteria", "allOf", []) | ||
]); | ||
}); | ||
|
||
test.run(); |