-
-
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
6d5a65f
commit 1265eac
Showing
8 changed files
with
228 additions
and
8 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,15 @@ | ||
export default addUndefinedAsNullKeyword; | ||
export type Ajv = import("ajv").default; | ||
export type SchemaValidateFunction = import("ajv").SchemaValidateFunction; | ||
export type AnySchemaObject = import("ajv").AnySchemaObject; | ||
export type ValidateFunction = import("ajv").ValidateFunction; | ||
/** @typedef {import("ajv").default} Ajv */ | ||
/** @typedef {import("ajv").SchemaValidateFunction} SchemaValidateFunction */ | ||
/** @typedef {import("ajv").AnySchemaObject} AnySchemaObject */ | ||
/** @typedef {import("ajv").ValidateFunction} ValidateFunction */ | ||
/** | ||
* | ||
* @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
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,39 @@ | ||
/** @typedef {import("ajv").default} Ajv */ | ||
/** @typedef {import("ajv").SchemaValidateFunction} SchemaValidateFunction */ | ||
/** @typedef {import("ajv").AnySchemaObject} AnySchemaObject */ | ||
/** @typedef {import("ajv").ValidateFunction} ValidateFunction */ | ||
|
||
/** | ||
* | ||
* @param {Ajv} ajv | ||
* @returns {Ajv} | ||
*/ | ||
function addUndefinedAsNullKeyword(ajv) { | ||
ajv.addKeyword({ | ||
keyword: "undefinedAsNull", | ||
before: "enum", | ||
modifying: true, | ||
/** @type {SchemaValidateFunction} */ | ||
validate(kwVal, data, metadata, dataCxt) { | ||
if ( | ||
kwVal && | ||
dataCxt && | ||
metadata && | ||
typeof metadata.enum !== "undefined" | ||
) { | ||
const idx = dataCxt.parentDataProperty; | ||
|
||
if (typeof dataCxt.parentData[idx] === "undefined") { | ||
// eslint-disable-next-line no-param-reassign | ||
dataCxt.parentData[dataCxt.parentDataProperty] = 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