Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for ajv-formats #31

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/nine-carrots-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nornir/core": minor
"@nornir/rest": minor
---

add support for ajv-formats
2 changes: 1 addition & 1 deletion packages/core/__tests__/src/attachment-registry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, jest } from "@jest/globals";
import { AttachmentKey, AttachmentRegistry, RegistryFactory } from "../../dist/lib/attachment-registry.js";
import { AttachmentRegistry, RegistryFactory } from "../../dist/lib/attachment-registry.js";
import { NornirMissingAttachmentException } from "../../dist/lib/error.js";

describe("AttachmentRegistry", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "John Conley",
"devDependencies": {
"@jest/globals": "^29.5.0",
"@nrfcloud/ts-json-schema-transformer": "^1.4.1",
"@nrfcloud/ts-json-schema-transformer": "^1.4.2",
"@types/jest": "^29.4.0",
"@types/node": "^18.15.11",
"esbuild": "^0.17.18",
Expand Down
1 change: 1 addition & 0 deletions packages/rest/__tests__/src/openapi-router.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@

router.implementRoute("/root/basepath/route/{reallyCool}", "post", chain =>
chain.use(request => {

return {
statusCode: "200",
headers: {
Expand Down Expand Up @@ -696,7 +697,7 @@
const router2 = OpenAPIRouter.fromSpec(TestSpec);

router2.implementRoute("/root/basepath/route/{reallyCool}", "get", chain =>
chain.use(request => {

Check warning on line 700 in packages/rest/__tests__/src/openapi-router.spec.mts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'request' is defined but never used. Allowed unused args must match /^_/u
return {
statusCode: "200",
headers: {
Expand Down
11 changes: 6 additions & 5 deletions packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^11.5.4",
"@nornir/core": "workspace:^",
"@nrfcloud/ts-json-schema-transformer": "^1.4.1",
"@nrfcloud/ts-json-schema-transformer": "^1.4.2",
"@types/aws-lambda": "^8.10.115",
"ajv": "^8.12.0",
"ajv": "^8.16.0",
"ajv-formats": "^3.0.1",
"atlassian-openapi": "^1.0.18",
"debug": "^4.3.4",
"glob": "^10.3.10",
"json-schema-to-ts": "^3.0.0",
"json-schema-to-ts": "^3.1.0",
"json-schema-traverse": "^1.0.0",
"lodash": "^4.17.21",
"openapi-diff": "^0.23.6",
"openapi-types": "^12.1.0",
"trouter": "^3.2.1",
"ts-is-present": "^1.2.2",
"ts-json-schema-generator": "^1.5.0",
"ts-morph": "^21.0.1",
"ts-json-schema-generator": "1.5.1",
"ts-morph": "^23.0.0",
"tsutils": "^3.21.0",
"yargs": "^17.7.2"
},
Expand Down
5 changes: 5 additions & 0 deletions packages/rest/src/runtime/http-event.mts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
NotExtended = "510",
}

type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type NrfcloudError<T extends HttpStatusCode> = `${T}${Digit}${Digit}`;

const test: NrfcloudError<HttpStatusCode.Accepted> = "20201"

Check warning on line 133 in packages/rest/src/runtime/http-event.mts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'test' is assigned a value but never used. Allowed unused vars must match /^_/u

/**
* @ignore
*/
Expand Down
8 changes: 5 additions & 3 deletions packages/rest/src/runtime/openapi/openapi-router.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {AttachmentRegistry, Nornir, Result} from "@nornir/core";
import {default as addFormats} from "ajv-formats"
import {OpenAPIV3_1} from "./spec.mjs"
import {
DereferenceSpec,
Expand Down Expand Up @@ -29,11 +30,12 @@ export class OpenAPIRouter<
const InputSpec,
const Spec extends OpenAPIV3_1.Document = DereferenceSpec<InputSpec>
> {
private static validator = new Ajv({
private static validator = addFormats.default(new Ajv({
allErrors: true,
coerceTypes: true,
strict: false
});
strict: false,
}));

private routes: {
path: string,
method: HttpMethod,
Expand Down
13 changes: 7 additions & 6 deletions packages/rest/src/transform/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Config,
createParser,
NodeParser,
ReferenceType,

Check warning on line 7 in packages/rest/src/transform/project.ts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'ReferenceType' is defined but never used. Allowed unused vars must match /^_/u
SchemaGenerator,
StringType,
SubNodeParser,
Expand Down Expand Up @@ -33,31 +33,32 @@

export type AJVOptions = Pick<
AJVBaseOptions,
"useDefaults" | "removeAdditional" | "loopRequired" | "loopEnum" | "allErrors"
"useDefaults" | "removeAdditional" | "loopRequired" | "loopEnum" | "allErrors" | "coerceTypes"
>;
export type SchemaConfig = Pick<
Config,
"sortProps" | "expose" | "jsDoc" | "strictTuples" | "encodeRefs" | "additionalProperties"
>;

export const AJV_DEFAULTS: AJVBaseOptions = {
export const AJV_DEFAULTS = {
useDefaults: true,
coerceTypes: true,
loopRequired: 20,
allErrors: false,
removeAdditional: true,
};
coerceTypes: true,
loopEnum: 20,
} satisfies Options;

Check warning on line 50 in packages/rest/src/transform/project.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

export const SCHEMA_DEFAULTS: Config = {
export const SCHEMA_DEFAULTS = {
expose: "export",
jsDoc: "extended",
sortProps: true,
strictTuples: false,
encodeRefs: false,
additionalProperties: false,
topRef: false,
discriminatorType: "open-api",
};
} satisfies Config;

Check warning on line 61 in packages/rest/src/transform/project.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

export type Options = AJVOptions & SchemaConfig;

Expand Down
8 changes: 4 additions & 4 deletions packages/rest/src/transform/transform.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
BaseType,
createFormatter,
createParser,

Check warning on line 4 in packages/rest/src/transform/transform.ts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'createParser' is defined but never used. Allowed unused vars must match /^_/u
Definition,
SchemaGenerator,
StringType,

Check warning on line 7 in packages/rest/src/transform/transform.ts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'StringType' is defined but never used. Allowed unused vars must match /^_/u
SubNodeParser,

Check warning on line 8 in packages/rest/src/transform/transform.ts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'SubNodeParser' is defined but never used. Allowed unused vars must match /^_/u
SubTypeFormatter,
UndefinedType,
} from "ts-json-schema-generator";
Expand Down Expand Up @@ -51,24 +51,24 @@
encodeRefs,
} = options ?? {};

const schemaConfig: SchemaConfig = {
const schemaConfig = {
...SCHEMA_DEFAULTS,
jsDoc: jsDoc ?? SCHEMA_DEFAULTS.jsDoc,
strictTuples: strictTuples ?? SCHEMA_DEFAULTS.strictTuples,
encodeRefs: encodeRefs ?? SCHEMA_DEFAULTS.encodeRefs,
additionalProperties: additionalProperties ?? SCHEMA_DEFAULTS.additionalProperties,
sortProps: sortProps ?? SCHEMA_DEFAULTS.sortProps,
expose,
};
} satisfies SchemaConfig;

Check warning on line 62 in packages/rest/src/transform/transform.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

const validationConfig: AJVOptions = {
const validationConfig = {
...AJV_DEFAULTS,
loopRequired: loopRequired ?? AJV_DEFAULTS.loopRequired,
loopEnum: loopEnum ?? AJV_DEFAULTS.loopEnum,
removeAdditional: removeAdditional ?? AJV_DEFAULTS.removeAdditional,
useDefaults: useDefaults ?? AJV_DEFAULTS.useDefaults,
allErrors: allErrors ?? AJV_DEFAULTS.allErrors,
};
} satisfies AJVOptions;

Check warning on line 71 in packages/rest/src/transform/transform.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

const nodeParser = getSchemaNodeParser(program, schemaConfig);
const typeFormatter = createFormatter({
Expand Down
2 changes: 1 addition & 1 deletion packages/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@nrfcloud/ts-json-schema-transformer": "^1.4.1",
"@nrfcloud/ts-json-schema-transformer": "^1.4.2",
"@types/aws-lambda": "^8.10.115",
"@types/jest": "^29.4.0",
"@types/node": "^18.15.11",
Expand Down
Loading
Loading