-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "feat: use ajv standalone validation code (#258)"
This reverts commit 2a69683.
- Loading branch information
1 parent
2a69683
commit 15bf048
Showing
7 changed files
with
851 additions
and
1,180 deletions.
There are no files selected for viewing
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
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,12 +1,27 @@ | ||
import { ValidateFunction } from "ajv/dist/core.js"; | ||
import strictCompiledSchema from "../__generated__/compiled_schema_strict.js"; | ||
import nonStrictCompiledSchema from "../__generated__/compiled_schema_non_strict.js"; | ||
import Ajv from "ajv"; | ||
import addFormats from "ajv-formats"; | ||
import openAttestationSchemav2 from "../2.0/schema/schema.json"; | ||
import openAttestationSchemav3 from "../3.0/schema/schema.json"; | ||
import { CurrentOptions } from "ajv/dist/core"; | ||
|
||
export const getSchema = (key: string, mode = "strict"): any => { | ||
const schema = | ||
mode === "strict" | ||
? strictCompiledSchema[key as keyof typeof strictCompiledSchema] | ||
: nonStrictCompiledSchema[key as keyof typeof nonStrictCompiledSchema]; | ||
const defaultTransform = (schema: Record<string, any>) => schema; | ||
export const buildAjv = ( | ||
options: CurrentOptions & { transform: (schema: Record<string, any>) => Record<string, any> } = { | ||
transform: defaultTransform, | ||
} | ||
): Ajv => { | ||
const { transform, ...ajvOptions } = options; | ||
const ajv = new Ajv({ allErrors: true, allowUnionTypes: true, ...ajvOptions }); | ||
addFormats(ajv); | ||
ajv.addKeyword("deprecationMessage"); | ||
ajv.compile(transform(openAttestationSchemav2)); | ||
ajv.compile(transform(openAttestationSchemav3)); | ||
return ajv; | ||
}; | ||
|
||
const localAjv = buildAjv(); | ||
export const getSchema = (key: string, ajv = localAjv) => { | ||
const schema = ajv.getSchema(key); | ||
if (!schema) throw new Error(`Could not find ${key} schema`); | ||
return schema as ValidateFunction; | ||
return schema; | ||
}; |
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