Skip to content

Commit

Permalink
Merge pull request #121 from Sphereon-Opensource/fix/WAL-635
Browse files Browse the repository at this point in the history
fix: used compiled schema for ajv
  • Loading branch information
sksadjad authored Jul 3, 2023
2 parents 1b59341 + edb1478 commit 618e1bf
Show file tree
Hide file tree
Showing 22 changed files with 1,060 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"parser": "@typescript-eslint/parser",
"parserOptions": { "project": "./tsconfig.json" },
"env": { "es6": true },
"ignorePatterns": ["node_modules", "dist", "coverage", "jest.config.js"],
"ignorePatterns": ["node_modules", "dist", "coverage", "jest.config.js", "resources"],
"plugins": ["import", "eslint-comments"],
"extends": [
"eslint:recommended",
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
node-version: 16
- name: yarn install
run: yarn install
- name: tsc
uses: icrawl/action-tsc@v1
- name: yarn build
run: yarn build
- name: tsc
uses: icrawl/action-tsc@v1
- name: yarn test
run: yarn test
- name: codecov
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ dist
node_modules
coverage
*.log
*.tgz
*.tgz
/lib/validation/*.js
/resources/FilterV1.schema.json
/resources/FilterV2.schema.json
2 changes: 2 additions & 0 deletions declarations.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare module 'lib/validation/validatePDv1.js';
declare module 'lib/validation/validatePDv2.js';
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class InputDescriptorFilterEvaluationHandler extends AbstractEvaluationHa
if (field.filter?.format && field.filter.format === 'date') {
this.transformDateFormat(result);
}
const ajv = new Ajv({ verbose: true, allowUnionTypes: true, allErrors: true, strict: false });
const ajv = new Ajv({ verbose: true, code: { source: true, lines: true, esm: false }, allowUnionTypes: true, allErrors: true, strict: false });
addFormats(ajv);
if (field.filter) {
return ajv.validate(field.filter, result.value);
Expand Down
15 changes: 8 additions & 7 deletions lib/utils/VCUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { AdditionalClaims, ICredential, ICredentialSubject, IIssuer } from '@sphereon/ssi-types';
import Ajv from 'ajv';

import { DiscoveredVersion, IPresentationDefinition, PEVersion } from '../types';
import { PresentationDefinitionSchema } from '../validation/core/presentationDefinitionSchema';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import validatePDv1 from '../validation/validatePDv1.js';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import validatePDv2 from '../validation/validatePDv2.js';

import { ObjectUtils } from './ObjectUtils';
import { JsonPathUtils } from './jsonPathUtils';
Expand All @@ -21,14 +25,11 @@ export function definitionVersionDiscovery(presentationDefinition: IPresentation
JsonPathUtils.changePropertyNameRecursively(presentationDefinitionCopy, '_const', 'const');
JsonPathUtils.changePropertyNameRecursively(presentationDefinitionCopy, '_enum', 'enum');
const data = { presentation_definition: presentationDefinitionCopy };
const ajv = new Ajv({ verbose: true, allowUnionTypes: true, allErrors: true, strict: false });
const validateV2 = ajv.compile(PresentationDefinitionSchema.getPresentationDefinitionSchemaV2());
let result = validateV2(data);
let result = validatePDv2(data);
if (result) {
return { version: PEVersion.v2 };
}
const validateV1 = ajv.compile(PresentationDefinitionSchema.getPresentationDefinitionSchemaV1());
result = validateV1(data);
result = validatePDv1(data);
if (result) {
return { version: PEVersion.v1 };
}
Expand Down
24 changes: 17 additions & 7 deletions lib/validation/bundlers/fieldsVB.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { FieldV1, FieldV2, FilterV1, FilterV2, Optionality } from '@sphereon/pex-models';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
// import Ajv from 'ajv';
// import addFormats from 'ajv-formats';
import jp from 'jsonpath';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { Validation, ValidationPredicate } from '../core';
import validateFilterv1 from '../validateFilterv1.js';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import validateFilterv2 from '../validateFilterv2.js';

import { ValidationBundler } from './validationBundler';

export class FieldsVB extends ValidationBundler<FieldV1[] | FieldV2[]> {
private readonly schemaValidator: Ajv;
// private readonly ajv: Ajv;

private readonly mustHaveValidJsonPathsMsg = 'field object "path" property must contain array of valid json paths';
private readonly pathObjMustHaveValidJsonPathMsg = 'field object "path" property must contain valid json paths.';
Expand All @@ -20,8 +26,8 @@ export class FieldsVB extends ValidationBundler<FieldV1[] | FieldV2[]> {

constructor(parentTag: string) {
super(parentTag, 'fields');
this.schemaValidator = new Ajv();
addFormats(this.schemaValidator);
// this.ajv = new Ajv();
// addFormats(this.ajv);
}

public getValidations(fields: FieldV1[] | FieldV2[]): Validation<FieldV1 | FieldV2>[] {
Expand Down Expand Up @@ -101,12 +107,16 @@ export class FieldsVB extends ValidationBundler<FieldV1[] | FieldV2[]> {
if (filter == null) {
return true;
}
let valid = false;
try {
this.schemaValidator.compile(filter);
valid = validateFilterv2(filter);
if (!valid) {
valid = validateFilterv1(filter);
}
} catch (err) {
throw this.toChecked(this.filterIsNotValidJsonSchemaDescriptorMsg + ' Got ' + JSON.stringify(filter));
}
return true;
return valid;
}

private filterIsMustInPresenceOfPredicate() {
Expand Down
13 changes: 4 additions & 9 deletions lib/validation/bundlers/presentationDefinitionV1VB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import {
PresentationDefinitionV1,
SubmissionRequirement,
} from '@sphereon/pex-models';
import Ajv from 'ajv';

import { Validation, ValidationPredicate } from '../core';
import { JwtAlgos } from '../core/jwtAlgos';
import { LdpTypes } from '../core/ldpTypes';
import { PresentationDefinitionSchema } from '../core/presentationDefinitionSchema';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import validatePDv1 from '../validatePDv1.js';

import { InputDescriptorsV1VB } from './inputDescriptorsV1VB';
import { SubmissionRequirementVB } from './submissionRequirementVB';
Expand All @@ -23,11 +24,8 @@ import { ValidationBundler } from './validationBundler';
export class PresentationDefinitionV1VB extends ValidationBundler<
FieldV1 | HolderSubject | ConstraintsV1 | InputDescriptorV1 | PresentationDefinitionV1 | SubmissionRequirement
> {
private ajv: Ajv;

constructor(parentTag: string) {
super(parentTag, 'presentation_definition');
this.ajv = new Ajv({ allowUnionTypes: true, verbose: false, strict: false });
}

public getValidations(
Expand Down Expand Up @@ -233,10 +231,7 @@ export class PresentationDefinitionV1VB extends ValidationBundler<
private shouldBeAsPerJsonSchema(): ValidationPredicate<PresentationDefinitionV1> {
// TODO can be be extracted as a generic function
return (presentationDefinition: PresentationDefinitionV1): boolean => {
const presentationDefinitionSchema = PresentationDefinitionSchema.getPresentationDefinitionSchemaV1();
const validate = this.ajv.compile(presentationDefinitionSchema);
const valid = validate({ presentation_definition: presentationDefinition });
return valid;
return validatePDv1({ presentation_definition: presentationDefinition });
};
}

Expand Down
13 changes: 4 additions & 9 deletions lib/validation/bundlers/presentationDefinitionV2VB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import {
PresentationDefinitionV2,
SubmissionRequirement,
} from '@sphereon/pex-models';
import Ajv from 'ajv';

import { Validation, ValidationPredicate } from '../core';
import { JwtAlgos } from '../core/jwtAlgos';
import { LdpTypes } from '../core/ldpTypes';
import { PresentationDefinitionSchema } from '../core/presentationDefinitionSchema';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import validatePDv2 from '../validatePDv2.js';

import { FrameVB } from './frameVB';
import { InputDescriptorsV2VB } from './inputDescriptorsV2VB';
Expand All @@ -24,11 +25,8 @@ import { ValidationBundler } from './validationBundler';
export class PresentationDefinitionV2VB extends ValidationBundler<
FieldV2 | HolderSubject | ConstraintsV2 | InputDescriptorV2 | PresentationDefinitionV2 | SubmissionRequirement
> {
private ajv: Ajv;

constructor(parentTag: string) {
super(parentTag, 'presentation_definition');
this.ajv = new Ajv({ verbose: true, allowUnionTypes: true, allErrors: true, strict: false });
}

public getValidations(
Expand Down Expand Up @@ -239,10 +237,7 @@ export class PresentationDefinitionV2VB extends ValidationBundler<
private shouldBeAsPerJsonSchema(): ValidationPredicate<PresentationDefinitionV2> {
// TODO can be be extracted as a generic function
return (presentationDefinition: PresentationDefinitionV2): boolean => {
const presentationDefinitionSchema = PresentationDefinitionSchema.getPresentationDefinitionSchemaV2();
const validate = this.ajv.compile(presentationDefinitionSchema);
const valid = validate({ presentation_definition: presentationDefinition });
return valid;
return validatePDv2({ presentation_definition: presentationDefinition });
};
}
}
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sphereon/pex",
"version": "2.0.2-unstable.0",
"version": "2.0.2-unstable.3",
"description": "A Typescript implementation of the v1 and v2 DIF Presentation Exchange specification",
"main": "dist/main/index.js",
"module": "dist/module/index.js",
Expand All @@ -17,15 +17,21 @@
"DIF"
],
"scripts": {
"build": "run-p build:*",
"build": "run-s build:*",
"build:generateFieldv1Schema": "ts-json-schema-generator -p resources/schema-generator-interfaces/filterV1.ts > resources/FilterV1.schema.json",
"build:generateFieldv2Schema": "ts-json-schema-generator -p resources/schema-generator-interfaces/filterV2.ts > resources/FilterV2.schema.json",
"build:pdv1schema": "npx ajv compile --strict=false --all-errors --inline-refs=false --verbose --allow-union-types -s resources/presentation_definition_v1.schema.json -o lib/validation/validatePDv1.js",
"build:pdv2schema": "npx ajv compile --strict=false --all-errors --inline-refs=false --verbose --allow-union-types -s resources/presentation_definition_v2.schema.json -o lib/validation/validatePDv2.js",
"build:filterV1schema": "npx ajv compile --strict=false --all-errors --inline-refs=false --verbose --allow-union-types -s resources/FilterV1.schema.json -o lib/validation/validateFilterv1.js",
"build:filterV2schema": "npx ajv compile --strict=false --all-errors --inline-refs=false --verbose --allow-union-types -s resources/FilterV2.schema.json -o lib/validation/validateFilterv2.js",
"build:main": "tsc -p tsconfig.main.json",
"build:browser": "tsc -p tsconfig.browser.json",
"build:module": "tsc -p tsconfig.module.json",
"clean": "rimraf node_modules dist coverage",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"{lib,test}/**/*.ts\" --write",
"fix:lint": "eslint . --ext .ts --fix",
"test": "run-s build test:*",
"test": "run-s test:*",
"test:lint": "eslint . --ext .ts",
"test:prettier": "prettier \"{lib,test}/**/*.ts\" --list-different",
"test:unit": "jest",
Expand All @@ -35,8 +41,8 @@
"node": ">=16"
},
"dependencies": {
"@sphereon/ssi-types": "^0.10.0",
"@sphereon/pex-models": "^2.0.2",
"@sphereon/ssi-types": "^0.10.0",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"jsonpath": "^1.1.1",
Expand All @@ -45,11 +51,13 @@
"string.prototype.matchall": "^4.0.8"
},
"devDependencies": {
"ajv-cli": "^5.0.0",
"@types/jest": "^29.4.0",
"@types/jsonpath": "^0.2.0",
"@types/node": "^16.18.25",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"ts-json-schema-generator": "^1.2.0",
"codecov": "^3.8.3",
"cspell": "^6.31.1",
"eslint": "^8.39.0",
Expand All @@ -67,6 +75,8 @@
},
"files": [
"index.ts",
"lib/validation/validatePDv1.js",
"lib/validation/validatePDv2.js",
"/dist",
"CHANGELOG.md",
"LICENSE",
Expand Down
Loading

0 comments on commit 618e1bf

Please sign in to comment.