-
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.
- Loading branch information
1 parent
a19ecd1
commit 53d677d
Showing
3 changed files
with
10 additions
and
62 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 |
---|---|---|
@@ -1,27 +1,8 @@ | ||
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"; | ||
import { AnyValidateFunction } from "ajv/dist/core.js"; | ||
import compiledSchema from "../__generated__/compiled_schema.js"; | ||
|
||
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); | ||
export const getSchema = (key: string): any => { | ||
const schema = compiledSchema[key as keyof typeof compiledSchema]; | ||
if (!schema) throw new Error(`Could not find ${key} schema`); | ||
return schema; | ||
return schema as AnyValidateFunction; | ||
}; |
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