-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d350b63
commit 95826eb
Showing
9 changed files
with
270 additions
and
11 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,8 @@ | ||
export default addUndefinedAsNullKeyword; | ||
export type Ajv = import("ajv").Ajv; | ||
/** | ||
* | ||
* @param {Ajv} ajv | ||
* @returns {Ajv} | ||
*/ | ||
declare function addUndefinedAsNullKeyword(ajv: Ajv): Ajv; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,96 @@ | ||
/** @typedef {import("ajv").Ajv} Ajv */ | ||
|
||
/** | ||
* | ||
* @param {Ajv} ajv | ||
* @param {string} keyword | ||
* @param {any} definition | ||
*/ | ||
function addKeyword(ajv, keyword, definition) { | ||
let customRuleCode; | ||
|
||
try { | ||
// @ts-ignore | ||
// eslint-disable-next-line global-require | ||
customRuleCode = require("ajv/lib/dotjs/custom"); | ||
|
||
// @ts-ignore | ||
const { RULES } = ajv; | ||
|
||
let ruleGroup; | ||
|
||
for (let i = 0; i < RULES.length; i++) { | ||
const rg = RULES[i]; | ||
|
||
if (typeof rg.type === "undefined") { | ||
ruleGroup = rg; | ||
break; | ||
} | ||
} | ||
|
||
const rule = { | ||
keyword, | ||
definition, | ||
custom: true, | ||
code: customRuleCode, | ||
implements: definition.implements, | ||
}; | ||
ruleGroup.rules.unshift(rule); | ||
RULES.custom[keyword] = rule; | ||
|
||
RULES.keywords[keyword] = true; | ||
RULES.all[keyword] = true; | ||
} catch (e) { | ||
// Nothing, fallback | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {Ajv} ajv | ||
* @returns {Ajv} | ||
*/ | ||
function addUndefinedAsNullKeyword(ajv) { | ||
// There is workaround for old versions of ajv, where `before` is not implemented | ||
addKeyword(ajv, "undefinedAsNull", { | ||
modifying: true, | ||
/** | ||
* @param {boolean} kwVal | ||
* @param {unknown} data | ||
* @param {any} parentSchema | ||
* @param {string} dataPath | ||
* @param {unknown} parentData | ||
* @param {number | string} parentDataProperty | ||
* @return {boolean} | ||
*/ | ||
validate( | ||
kwVal, | ||
data, | ||
parentSchema, | ||
dataPath, | ||
parentData, | ||
parentDataProperty | ||
) { | ||
if ( | ||
kwVal && | ||
parentSchema && | ||
typeof parentSchema.enum !== "undefined" && | ||
parentData && | ||
typeof parentDataProperty === "number" | ||
) { | ||
const idx = /** @type {number} */ (parentDataProperty); | ||
const parentDataRef = /** @type {any[]} */ (parentData); | ||
|
||
if (typeof parentDataRef[idx] === "undefined") { | ||
parentDataRef[idx] = null; | ||
} | ||
} | ||
|
||
return true; | ||
}, | ||
}); | ||
|
||
return ajv; | ||
} | ||
|
||
export default addUndefinedAsNullKeyword; |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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