Skip to content

Commit

Permalink
Merge pull request #31 from jfrconley/joco/add-ajv-formats
Browse files Browse the repository at this point in the history
add support for ajv-formats
  • Loading branch information
jfrconley authored Jun 25, 2024
2 parents 91a37d6 + d07637b commit cc67ce8
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 74 deletions.
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 @@ describe("OpenAPI Router", () => {

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

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 @@ export enum HttpStatusCode {
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"

/**
* @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 @@ -33,22 +33,23 @@ export interface Project {

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;

export const SCHEMA_DEFAULTS: Config = {
export const SCHEMA_DEFAULTS = {
expose: "export",
jsDoc: "extended",
sortProps: true,
Expand All @@ -57,7 +58,7 @@ export const SCHEMA_DEFAULTS: Config = {
additionalProperties: false,
topRef: false,
discriminatorType: "open-api",
};
} satisfies Config;

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
Expand Up @@ -51,24 +51,24 @@ export function transform(program: ts.Program, options?: Options): ts.Transforme
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;

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;

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

0 comments on commit cc67ce8

Please sign in to comment.