@dvsa/smc-encounter-schema 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +31 -0
- package/biome.json +3 -0
- package/chunk-FKJCRL2J.mjs +1 -0
- package/index.d.mts +17 -0
- package/index.d.ts +17 -0
- package/index.js +1 -0
- package/index.mjs +1 -0
- package/jest.config.ts +8 -0
- package/models/request/EncounterReq.d.mts +5 -0
- package/models/request/EncounterReq.d.ts +5 -0
- package/models/request/EncounterReq.js +1 -0
- package/models/request/EncounterReq.mjs +1 -0
- package/package.json +47 -0
- package/src/index.ts +35 -0
- package/src/models/__mocks__/invalid/missing-mandatory.json +1 -0
- package/src/models/__mocks__/valid/complete-encounter.json +3 -0
- package/src/models/__tests__/index.spec.ts +20 -0
- package/src/models/request/EncounterReq.ts +7 -0
- package/tsconfig.json +19 -0
- package/tsup.config.ts +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Driver & Vehicle Standards Agency
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Encounter Schema
|
|
2
|
+
|
|
3
|
+
Schemas required for Encounter submission as well as custom validators
|
|
4
|
+
## Developing
|
|
5
|
+
### Pre-requisites
|
|
6
|
+
|
|
7
|
+
- Node.js (Please see `.nvmrc` for specific version)
|
|
8
|
+
- `npm` (If using [n](https://github.com/tj/n) or [nvm](https://github.com/nvm-sh/nvm), this will be automatically managed)
|
|
9
|
+
- Security
|
|
10
|
+
- [Git secrets](https://github.com/awslabs/git-secrets)
|
|
11
|
+
- [ScanRepo](https://github.com/UKHomeOffice/repo-security-scanner)
|
|
12
|
+
- Unzip `repo-security-scanner_<version>_Darwin_<architercture>.tar.gz` and rename the executable inside the folder
|
|
13
|
+
to `scanrepo` - Add executable to path (using `echo $PATH` to find your path)
|
|
14
|
+
|
|
15
|
+
### Getting started
|
|
16
|
+
|
|
17
|
+
###### Run the following command after cloning the project
|
|
18
|
+
|
|
19
|
+
1. `npm install` (or `npm i`)
|
|
20
|
+
|
|
21
|
+
###### The code that will be published lives inside the ./src directory.
|
|
22
|
+
|
|
23
|
+
To simplify the publishing step, each file must be exported from the `index.ts` file.
|
|
24
|
+
|
|
25
|
+
### Publishing
|
|
26
|
+
|
|
27
|
+
To see the output of what will be published, run the following command:
|
|
28
|
+
|
|
29
|
+
```shell
|
|
30
|
+
npm publish --dry-run
|
|
31
|
+
```
|
package/biome.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {IsNotEmpty,IsString}from'class-validator';var I=Object.defineProperty;var o=(m,s,g,t)=>{for(var r=void 0,i=m.length-1,p;i>=0;i--)(p=m[i])&&(r=(p(s,g,r))||r);return r&&I(s,g,r),r};var a=class{generatedNumber};o([IsNotEmpty(),IsString()],a.prototype,"generatedNumber");export{a};
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as class_validator from 'class-validator';
|
|
2
|
+
import { ValidatorOptions } from 'class-validator/types/validation/ValidatorOptions';
|
|
3
|
+
import { EncounterReq } from './models/request/EncounterReq.mjs';
|
|
4
|
+
|
|
5
|
+
declare abstract class ValidatorBase {
|
|
6
|
+
private static _encounterReq;
|
|
7
|
+
protected constructor(encounterReq: Readonly<Partial<EncounterReq>>);
|
|
8
|
+
static get encounterReq(): Readonly<Partial<EncounterReq>>;
|
|
9
|
+
}
|
|
10
|
+
declare class EncounterSchema extends ValidatorBase {
|
|
11
|
+
private validationOptions;
|
|
12
|
+
private static readonly _defaultValidatorOptions;
|
|
13
|
+
constructor(encounterReq: Partial<EncounterReq>, validationOptions?: ValidatorOptions);
|
|
14
|
+
validate(): Promise<class_validator.ValidationError[]>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { EncounterSchema, ValidatorBase };
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as class_validator from 'class-validator';
|
|
2
|
+
import { ValidatorOptions } from 'class-validator/types/validation/ValidatorOptions';
|
|
3
|
+
import { EncounterReq } from './models/request/EncounterReq.js';
|
|
4
|
+
|
|
5
|
+
declare abstract class ValidatorBase {
|
|
6
|
+
private static _encounterReq;
|
|
7
|
+
protected constructor(encounterReq: Readonly<Partial<EncounterReq>>);
|
|
8
|
+
static get encounterReq(): Readonly<Partial<EncounterReq>>;
|
|
9
|
+
}
|
|
10
|
+
declare class EncounterSchema extends ValidatorBase {
|
|
11
|
+
private validationOptions;
|
|
12
|
+
private static readonly _defaultValidatorOptions;
|
|
13
|
+
constructor(encounterReq: Partial<EncounterReq>, validationOptions?: ValidatorOptions);
|
|
14
|
+
validate(): Promise<class_validator.ValidationError[]>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { EncounterSchema, ValidatorBase };
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';require('reflect-metadata');var classTransformer=require('class-transformer'),classValidator=require('class-validator');var d=Object.defineProperty;var p=(t,a,e,o)=>{for(var r=void 0,s=t.length-1,l;s>=0;s--)(l=t[s])&&(r=(l(a,e,r))||r);return r&&d(a,e,r),r};var n=class{generatedNumber};p([classValidator.IsNotEmpty(),classValidator.IsString()],n.prototype,"generatedNumber");var i=class t{static _encounterReq;constructor(a){t._encounterReq=a;}static get encounterReq(){return t._encounterReq}},c=class t extends i{constructor(e,o=t._defaultValidatorOptions){super(e);this.validationOptions=o;}static _defaultValidatorOptions={validationError:{target:false,value:false}};async validate(){let e=classTransformer.plainToInstance(n,i.encounterReq);return await classValidator.validate(e,this.validationOptions)}};exports.EncounterSchema=c;exports.ValidatorBase=i;
|
package/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {a as a$1}from'./chunk-FKJCRL2J.mjs';import'reflect-metadata';import {plainToInstance}from'class-transformer';import {validate}from'class-validator';var a=class t{static _encounterReq;constructor(o){t._encounterReq=o;}static get encounterReq(){return t._encounterReq}},n=class t extends a{constructor(e,s=t._defaultValidatorOptions){super(e);this.validationOptions=s;}static _defaultValidatorOptions={validationError:{target:false,value:false}};async validate(){let e=plainToInstance(a$1,a.encounterReq);return await validate(e,this.validationOptions)}};export{n as EncounterSchema,a as ValidatorBase};
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var classValidator=require('class-validator');var I=Object.defineProperty;var o=(m,s,g,t)=>{for(var r=void 0,i=m.length-1,p;i>=0;i--)(p=m[i])&&(r=(p(s,g,r))||r);return r&&I(s,g,r),r};var a=class{generatedNumber};o([classValidator.IsNotEmpty(),classValidator.IsString()],a.prototype,"generatedNumber");exports.EncounterReq=a;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{a as EncounterReq}from'../../chunk-FKJCRL2J.mjs';
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dvsa/smc-encounter-schema",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"author": "DVSA",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"keywords": ["dvsa", "nodejs", "typescript"],
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"directory": "dist",
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "npm run clean && tsup",
|
|
13
|
+
"clean": "rimraf coverage dist && npm run clean:temp",
|
|
14
|
+
"clean:temp": "rimraf api classes response",
|
|
15
|
+
"gitSecrets": "git secrets --scan . && git log -p -- . | scanrepo",
|
|
16
|
+
"lint": "biome check src",
|
|
17
|
+
"lint:fix": "npm run lint -- --write",
|
|
18
|
+
"prepare": "husky",
|
|
19
|
+
"test": "jest",
|
|
20
|
+
"test:coverage": "npm run test -- --coverage",
|
|
21
|
+
"build:package": "npm run build",
|
|
22
|
+
"prepublishOnly": "npm run build:package && cp -r ./dist/* . && rm -rf ./dist",
|
|
23
|
+
"postpublish": "git clean -fd && npm run clean:temp"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"class-validator": "^0.14.0",
|
|
27
|
+
"class-transformer": "^0.5.1",
|
|
28
|
+
"reflect-metadata": "^0.2.2"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@biomejs/biome": "1.9.4",
|
|
32
|
+
"@dvsa/biome-config": "0.4.0",
|
|
33
|
+
"@types/jest": "^29.5.14",
|
|
34
|
+
"@types/node": "^22.15.16",
|
|
35
|
+
"husky": "^9.1.7",
|
|
36
|
+
"jest": "^29.7.0",
|
|
37
|
+
"lint-staged": "^16.1.0",
|
|
38
|
+
"rimraf": "^6.0.1",
|
|
39
|
+
"ts-jest": "^29.3.4",
|
|
40
|
+
"ts-node": "^10.9.2",
|
|
41
|
+
"tsup": "^8.5.0",
|
|
42
|
+
"typescript": "^5.8.3"
|
|
43
|
+
},
|
|
44
|
+
"lint-staged": {
|
|
45
|
+
"*.{js,ts,mjs,css,md,ts,json}": "npm run lint:fix --ws -- --no-errors-on-unmatched"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { plainToInstance } from 'class-transformer';
|
|
3
|
+
import { validate as validateEncounter } from 'class-validator';
|
|
4
|
+
import type { ValidatorOptions } from 'class-validator/types/validation/ValidatorOptions';
|
|
5
|
+
import { EncounterReq } from './models/request/EncounterReq';
|
|
6
|
+
|
|
7
|
+
export abstract class ValidatorBase {
|
|
8
|
+
private static _encounterReq: Readonly<Partial<EncounterReq>>;
|
|
9
|
+
|
|
10
|
+
protected constructor(encounterReq: Readonly<Partial<EncounterReq>>) {
|
|
11
|
+
ValidatorBase._encounterReq = encounterReq;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static get encounterReq(): Readonly<Partial<EncounterReq>> {
|
|
15
|
+
return ValidatorBase._encounterReq;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class EncounterSchema extends ValidatorBase {
|
|
20
|
+
private static readonly _defaultValidatorOptions: ValidatorOptions = {
|
|
21
|
+
validationError: { target: false, value: false },
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
encounterReq: Partial<EncounterReq>,
|
|
26
|
+
private validationOptions: ValidatorOptions = EncounterSchema._defaultValidatorOptions
|
|
27
|
+
) {
|
|
28
|
+
super(encounterReq);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async validate() {
|
|
32
|
+
const encounterReqInstance = plainToInstance(EncounterReq, ValidatorBase.encounterReq);
|
|
33
|
+
return await validateEncounter(encounterReqInstance, this.validationOptions);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { EncounterSchema } from '../../index';
|
|
2
|
+
import * as MissingMandatory from '../__mocks__/invalid/missing-mandatory.json';
|
|
3
|
+
import * as CompleteEncounter from '../__mocks__/valid/complete-encounter.json';
|
|
4
|
+
|
|
5
|
+
describe('Encounter Schema Tests', () => {
|
|
6
|
+
describe('Valid encounters', () => {
|
|
7
|
+
it('should return 0 errors for a valid encounter', async () => {
|
|
8
|
+
const encounterSchema = new EncounterSchema(CompleteEncounter);
|
|
9
|
+
expect(await encounterSchema.validate()).toHaveLength(0);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('Invalid encounters', () => {
|
|
14
|
+
it('should return errors for an invalid encounter', async () => {
|
|
15
|
+
const encounterSchema = new EncounterSchema(MissingMandatory);
|
|
16
|
+
const validationErrors = await encounterSchema.validate();
|
|
17
|
+
expect(validationErrors).toHaveLength(1);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": ["ES2022"],
|
|
5
|
+
"experimentalDecorators": true,
|
|
6
|
+
"emitDecoratorMetadata": true,
|
|
7
|
+
"module": "CommonJS",
|
|
8
|
+
"types": ["node", "jest"],
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"outDir": "./dist",
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"resolveJsonModule": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true,
|
|
14
|
+
"strict": true,
|
|
15
|
+
"skipLibCheck": true
|
|
16
|
+
},
|
|
17
|
+
"include": ["src/**/*.ts"],
|
|
18
|
+
"exclude": ["node_modules", "src/**/*.spec.ts", "src/**/*.spec.d.ts"]
|
|
19
|
+
}
|