-
-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: $dataMetaSchema now uses $data in keyword definitions
- Loading branch information
1 parent
9f6a541
commit 057cdfc
Showing
3 changed files
with
14 additions
and
44 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
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 |
---|---|---|
@@ -1,54 +1,29 @@ | ||
// TODO use $data in keyword definitions | ||
const KEYWORDS = [ | ||
"multipleOf", | ||
"maximum", | ||
"exclusiveMaximum", | ||
"minimum", | ||
"exclusiveMinimum", | ||
"maxLength", | ||
"minLength", | ||
"pattern", | ||
"additionalItems", | ||
"maxItems", | ||
"minItems", | ||
"uniqueItems", | ||
"maxProperties", | ||
"minProperties", | ||
"required", | ||
"additionalProperties", | ||
"enum", | ||
"format", | ||
"const", | ||
] | ||
|
||
export default function $dataMetaSchema( | ||
this, | ||
metaSchema: object, | ||
keywordsJsonPointers: string[] | ||
): object { | ||
const rules = this.RULES.all | ||
for (const jsonPointer of keywordsJsonPointers) { | ||
metaSchema = JSON.parse(JSON.stringify(metaSchema)) | ||
const segments = jsonPointer.split("/").slice(1) // first segment is an empty string | ||
let keywords = metaSchema | ||
for (const seg of segments) keywords = keywords[seg] | ||
|
||
for (const key of KEYWORDS) { | ||
for (const key in rules) { | ||
const $data = rules[key]?.definition?.$data | ||
const schema = keywords[key] | ||
if (schema) keywords[key] = schemaOrData(schema) | ||
if ($data && schema) keywords[key] = schemaOrData(schema) | ||
} | ||
} | ||
|
||
return metaSchema | ||
} | ||
|
||
function schemaOrData(schema: object): object { | ||
return { | ||
anyOf: [ | ||
schema, | ||
{ | ||
$ref: "https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/refs/data.json#", | ||
}, | ||
], | ||
} | ||
const $dataRef = { | ||
$ref: "https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/refs/data.json#", | ||
} | ||
|
||
module.exports = $dataMetaSchema | ||
export function schemaOrData(schema: object | boolean): object { | ||
return {anyOf: [schema, $dataRef]} | ||
} |
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