Skip to content

Commit

Permalink
chore: remove ajv compile
Browse files Browse the repository at this point in the history
  • Loading branch information
zixiang2018 committed Oct 26, 2023
1 parent a19ecd1 commit 53d677d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 62 deletions.
29 changes: 5 additions & 24 deletions src/shared/ajv.ts
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;
};
38 changes: 2 additions & 36 deletions src/shared/utils/diagnose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
VerifiableCredentialWrappedProofStrict,
} from "../../3.0/types";
import { ArrayProof, Signature, SignatureStrict } from "../../2.0/types";
import { clone, cloneDeepWith } from "lodash";
import { buildAjv, getSchema } from "../ajv";
import { getSchema } from "../ajv";
import { Kind, Mode } from "./@types/diagnose";

type Version = "2.0" | "3.0";
Expand All @@ -26,35 +25,6 @@ const handleError = (debug: boolean, ...messages: string[]) => {
return messages.map((message) => ({ message }));
};

// remove enum and pattern from the schema
function transformSchema(schema: Record<string, any>): Record<string, any> {
const excludeKeys = ["enum", "pattern"];
function omit(value: any) {
if (value && typeof value === "object") {
const key = excludeKeys.find((key) => value[key]);
if (key) {
const node = clone(value);
excludeKeys.forEach((key) => {
delete node[key];
});
return node;
}
}
}

const newSchema = cloneDeepWith(schema, omit);
// because we remove check on enum (DNS-DID, DNS-TXT, etc.) the identity proof can match multiple sub schema in v2.
// so here we change oneOf to anyOf, so that if more than one identityProof matches, it still passes
if (newSchema?.definitions?.identityProof?.oneOf) {
newSchema.definitions.identityProof.anyOf = newSchema?.definitions?.identityProof?.oneOf;
delete newSchema?.definitions?.identityProof?.oneOf;
}
return newSchema;
}
// custom ajv for loose schema validation
// it will allow invalid format, invalid pattern and invalid enum
const ajv = buildAjv({ transform: transformSchema, validateFormats: false });

/**
* Tools to give information about the validity of a document. It will return and eventually output the errors found.
* @param version 2.0 or 3.0
Expand Down Expand Up @@ -84,11 +54,7 @@ export const diagnose = ({
return handleError(debug, "The document must be an object");
}

const errors = validate(
document,
getSchema(version === "3.0" ? SchemaId.v3 : SchemaId.v2, mode === "non-strict" ? ajv : undefined),
kind
);
const errors = validate(document, getSchema(version === "3.0" ? SchemaId.v3 : SchemaId.v2), kind);

if (errors.length > 0) {
// TODO this can be improved later
Expand Down
5 changes: 3 additions & 2 deletions src/shared/validate/validate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ErrorObject, ValidateFunction } from "ajv";
import { getLogger } from "../logger";
import { SchemaId } from "../@types/document";
// don't change this otherwise there is a cycle
import { getData } from "../utils/utils";
import { Kind } from "../utils/@types/diagnose";
import { AnyValidateFunction } from "ajv/dist/core";
import { ErrorObject } from "ajv";
const logger = getLogger("validate");

export const validateSchema = (document: any, validator: ValidateFunction, kind?: Kind): ErrorObject[] => {
export const validateSchema = (document: any, validator: AnyValidateFunction, kind?: Kind): ErrorObject[] => {
if (!validator) {
throw new Error("No schema validator provided");
}
Expand Down

0 comments on commit 53d677d

Please sign in to comment.