Skip to content

Commit

Permalink
feat(data-exchange-standard): moving standard schema to library
Browse files Browse the repository at this point in the history
  • Loading branch information
tzuge committed Aug 29, 2024
1 parent 0f73ab0 commit d5a9b1b
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 24 deletions.
18 changes: 0 additions & 18 deletions apps/form-service/src/standard.v1.schema.spec.ts

This file was deleted.

3 changes: 1 addition & 2 deletions apps/form-service/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"],
"resolveJsonModule": true
"types": ["jest", "node"]
},
"include": [
"**/*.spec.ts",
Expand Down
2 changes: 1 addition & 1 deletion apps/tenant-management-webapp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
{
"glob": "standard.*.schema.json",
"input": "apps/form-service/src",
"input": "libs/data-exchange-standard/src",
"output": "./"
}
],
Expand Down
3 changes: 3 additions & 0 deletions libs/core-common/src/jsonSchema/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { standardJsonSchema } from '@abgov/data-exchange-standard';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import * as schemaMigration from 'json-schema-migrate';
Expand All @@ -12,6 +13,8 @@ export class AjvValidationService implements ValidationService {
constructor(private logger: Logger) {
this.ajv.addFormat('file-urn', /^urn:ads:platform:file-service:v[0-9]:\/files\/[a-zA-Z0-9.-]*$/);
addFormats(this.ajv);

this.ajv.addSchema(standardJsonSchema);
}

setSchema(schemaKey: string, schema: Record<string, unknown>): void {
Expand Down
18 changes: 18 additions & 0 deletions libs/data-exchange-standard/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions libs/data-exchange-standard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# data-exchange-standard

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test data-exchange-standard` to execute the unit tests via [Jest](https://jestjs.io).
11 changes: 11 additions & 0 deletions libs/data-exchange-standard/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable */
export default {
displayName: 'data-exchange-standard',
preset: '../../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/data-exchange-standard',
};
19 changes: 19 additions & 0 deletions libs/data-exchange-standard/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "data-exchange-standard",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/data-exchange-standard/src",
"projectType": "library",
"tags": [],
"targets": {
"lint": {
"executor": "@nx/eslint:lint"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/data-exchange-standard/jest.config.ts"
}
}
}
}
3 changes: 3 additions & 0 deletions libs/data-exchange-standard/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as standardJsonSchema from './standard.v1.schema.json';

export { standardJsonSchema };
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"type": "object",
"properties": {
"postalAddress": {
"id": "#postalAddress",
"$id": "#postalAddress",
"description": "Address line contains the primary address number, predirectional information, street name, suffix, postdirectional information, secondary address identifier and/or secondary address.The address line can contain information for a civic, rural, or postal box address.",
"type": "object",
"properties": {
"addressLine1": {
"$ref": "#/$defs/addressLine"
Expand Down Expand Up @@ -59,8 +60,9 @@
"required": [
"address1",
"subdivisionCode",
"postalCode"
"postalCode",
"country"
]
}
}
}
}
44 changes: 44 additions & 0 deletions libs/data-exchange-standard/src/standard.v1.schema.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Ajv from 'ajv';
import * as standard from './standard.v1.schema.json';

describe('standard.schema.v1', () => {
it('is valid json schema', () => {
const ajv = new Ajv({ allErrors: true, verbose: true, strict: 'log' });
const result = ajv.validateSchema(standard);
expect(result).toBe(true);
});

it('can be referenced to validate', () => {
const ajv = new Ajv({ allErrors: true, verbose: true, strict: 'log' });
ajv.addSchema(standard);
ajv.addSchema(
{
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
address: { $ref: 'https:/adsp.alberta.ca/standard.v1.schema.json#postalAddress' },
},
},
'form'
);

let result = ajv.validate('form', {
address: {
address1: '123 fake st.',
subdivisionCode: 'AB',
postalCode: 'T1A 4L1',
country: 'CA',
},
});
expect(result).toBe(true);

result = ajv.validate('form', {
address: {
subdivisionCode: 'AB',
postalCode: 'T1A 4L1',
country: 'CA',
},
});
expect(result).toBe(false);
});
});
22 changes: 22 additions & 0 deletions libs/data-exchange-standard/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions libs/data-exchange-standard/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
9 changes: 9 additions & 0 deletions libs/data-exchange-standard/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
}
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
"module": "esnext",
"typeRoots": ["node_modules/@types"],
"lib": ["es2017", "dom"],
"resolveJsonModule": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@abgov/adsp-service-sdk": ["libs/adsp-service-sdk/src/index.ts"],
"@abgov/data-exchange-standard": ["libs/data-exchange-standard/src/index.ts"],
"@abgov/jsonforms-components": ["libs/jsonforms-components/src/index.ts"],
"@core-services/app-common": ["libs/app-common/src/index.ts"],
"@core-services/core-common": ["libs/core-common/src/index.ts"],
Expand Down

0 comments on commit d5a9b1b

Please sign in to comment.