forked from Open-Attestation/open-attestation
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
95 additions
and
20 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
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,27 @@ | ||
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"; | ||
|
||
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; | ||
}; |
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