@kaapi/validator-arktype 0.0.33 → 0.0.34
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/lib/{chunk-ML2GHWVG.js → chunk-QLKH743P.js} +74 -2
- package/lib/chunk-QLKH743P.js.map +1 -0
- package/lib/{chunk-4J6FCHLO.js → chunk-U7WK3Q5P.js} +4 -4
- package/lib/chunk-U7WK3Q5P.js.map +1 -0
- package/lib/doc-helpers.d.ts +2 -0
- package/lib/doc-helpers.js +1 -1
- package/lib/index.js +2 -2
- package/lib/metafile-esm.json +1 -1
- package/lib/validator.js +2 -2
- package/package.json +3 -3
- package/lib/chunk-4J6FCHLO.js.map +0 -1
- package/lib/chunk-ML2GHWVG.js.map +0 -1
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
// src/doc-helpers.ts
|
|
2
2
|
import { OpenAPIJsonHelper, PostmanJsonHelper } from "@novice1/api-doc-json-helper";
|
|
3
3
|
import { type, Type } from "arktype";
|
|
4
|
+
function reformatAnyOf(schema) {
|
|
5
|
+
if ("anyOf" in schema) {
|
|
6
|
+
const propertiesToCheck = [];
|
|
7
|
+
if (!schema.description) {
|
|
8
|
+
propertiesToCheck.push("description");
|
|
9
|
+
}
|
|
10
|
+
if (!schema.examples) {
|
|
11
|
+
propertiesToCheck.push("examples");
|
|
12
|
+
}
|
|
13
|
+
if (!schema.format) {
|
|
14
|
+
propertiesToCheck.push("format");
|
|
15
|
+
}
|
|
16
|
+
const objects = schema.anyOf;
|
|
17
|
+
for (const prop of propertiesToCheck) {
|
|
18
|
+
const firstElement = objects[0];
|
|
19
|
+
if (firstElement && typeof firstElement === "object") {
|
|
20
|
+
const firstValue = firstElement[prop];
|
|
21
|
+
const arraysEqual = (a, b) => {
|
|
22
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return false;
|
|
23
|
+
if (a.length !== b.length) return false;
|
|
24
|
+
const sortedA = [...a].sort();
|
|
25
|
+
const sortedB = [...b].sort();
|
|
26
|
+
return sortedA.every((val, i) => val === sortedB[i]);
|
|
27
|
+
};
|
|
28
|
+
const allSame = objects.every((obj) => {
|
|
29
|
+
const val = obj[prop];
|
|
30
|
+
if (Array.isArray(firstValue) && Array.isArray(val)) {
|
|
31
|
+
return arraysEqual(firstValue, val);
|
|
32
|
+
}
|
|
33
|
+
return val === firstValue;
|
|
34
|
+
});
|
|
35
|
+
if (allSame) {
|
|
36
|
+
if (prop === "description" && typeof firstValue === "string") {
|
|
37
|
+
schema.description = firstValue;
|
|
38
|
+
} else if (prop === "examples" && Array.isArray(firstValue)) {
|
|
39
|
+
schema.examples = firstValue;
|
|
40
|
+
} else if (prop === "format" && typeof firstValue === "string") {
|
|
41
|
+
schema.format = firstValue;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
4
48
|
function transformValue(value) {
|
|
5
49
|
let r = value;
|
|
6
50
|
if (value && typeof value === "function" && "toJsonSchema" in value && typeof value.toJsonSchema === "function") {
|
|
@@ -30,6 +74,21 @@ function transformValue(value) {
|
|
|
30
74
|
}
|
|
31
75
|
}
|
|
32
76
|
}
|
|
77
|
+
if ("properties" in r2) {
|
|
78
|
+
const r22 = value.toJSON();
|
|
79
|
+
if ("in" in r22 && r22.in && typeof r22.in === "object" && "optional" in r22.in) {
|
|
80
|
+
if (Array.isArray(r22.in.optional)) {
|
|
81
|
+
for (const prop of r22.in.optional) {
|
|
82
|
+
if (typeof prop === "object" && prop && "key" in prop && typeof prop.key === "string" && typeof r2.properties[prop.key].default === "undefined" && "default" in prop && typeof prop.default !== "undefined") {
|
|
83
|
+
r2.properties[prop.key].default = prop.default;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (const prop in r2.properties) {
|
|
89
|
+
reformatAnyOf(r2.properties[prop]);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
33
92
|
return r2;
|
|
34
93
|
}
|
|
35
94
|
});
|
|
@@ -89,7 +148,7 @@ var OpenAPIArkHelper = class _OpenAPIArkHelper extends OpenAPIJsonHelper {
|
|
|
89
148
|
let r = super.isRequired();
|
|
90
149
|
if (!r && this._originalSchema) {
|
|
91
150
|
const schema = this._schema;
|
|
92
|
-
if ("required" in schema && Array.isArray(schema.required) && "properties" in schema && typeof schema.properties === "object" && schema.properties && schema.required.length
|
|
151
|
+
if ("required" in schema && Array.isArray(schema.required) && "properties" in schema && typeof schema.properties === "object" && schema.properties && schema.required.length <= Object.keys(schema.properties).length) {
|
|
93
152
|
r = true;
|
|
94
153
|
}
|
|
95
154
|
}
|
|
@@ -135,10 +194,23 @@ var OpenAPIArkHelper = class _OpenAPIArkHelper extends OpenAPIJsonHelper {
|
|
|
135
194
|
var PostmanArkHelper = class _PostmanArkHelper extends PostmanJsonHelper {
|
|
136
195
|
constructor(params, isRequired) {
|
|
137
196
|
super({ ...params, value: transformValue(params.value) }, isRequired);
|
|
197
|
+
if (params.value instanceof Type) {
|
|
198
|
+
this._originalSchema = params.value;
|
|
199
|
+
}
|
|
138
200
|
}
|
|
139
201
|
isValid() {
|
|
140
202
|
return !!(this._schema && typeof this._schema === "object" && !("~standard" in this._schema) && ("type" in this._schema && typeof this._schema.type === "string" || "oneOf" in this._schema && Array.isArray(this._schema.oneOf) || "anyOf" in this._schema && Array.isArray(this._schema.anyOf) || "enum" in this._schema && Array.isArray(this._schema.enum)));
|
|
141
203
|
}
|
|
204
|
+
isRequired() {
|
|
205
|
+
let r = super.isRequired();
|
|
206
|
+
if (!r && this._originalSchema) {
|
|
207
|
+
const schema = this._schema;
|
|
208
|
+
if ("required" in schema && Array.isArray(schema.required) && "properties" in schema && typeof schema.properties === "object" && schema.properties && schema.required.length <= Object.keys(schema.properties).length) {
|
|
209
|
+
r = true;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return r;
|
|
213
|
+
}
|
|
142
214
|
getFirstItem() {
|
|
143
215
|
const schema = this._schema;
|
|
144
216
|
if ("items" in schema && typeof schema.items === "object") {
|
|
@@ -174,4 +246,4 @@ export {
|
|
|
174
246
|
OpenAPIArkHelper,
|
|
175
247
|
PostmanArkHelper
|
|
176
248
|
};
|
|
177
|
-
//# sourceMappingURL=chunk-
|
|
249
|
+
//# sourceMappingURL=chunk-QLKH743P.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/doc-helpers.ts"],"sourcesContent":["import type { KaapiOpenAPIHelperInterface } from '@kaapi/kaapi';\nimport { OpenAPIJsonHelper, PostmanJsonHelper } from '@novice1/api-doc-json-helper';\nimport { type, Type, type JsonSchema } from 'arktype';\n\n/**\n * reformat anyOf to externalize common values for: \n * - examples\n * - format\n * - description\n */\nfunction reformatAnyOf(schema: JsonSchema) {\n if ('anyOf' in schema) {\n const propertiesToCheck: ('description' | 'examples' | 'format')[] = [];\n if (!schema.description) {\n propertiesToCheck.push('description')\n }\n if (!schema.examples) {\n propertiesToCheck.push('examples')\n }\n if (!schema.format) {\n propertiesToCheck.push('format')\n }\n const objects = schema.anyOf;\n for (const prop of propertiesToCheck) {\n // Get the value from the first object\n const firstElement = objects[0]\n if (firstElement && typeof firstElement === 'object') {\n const firstValue = firstElement[prop];\n\n // Helper to compare arrays ignoring order\n const arraysEqual = (a: unknown, b: unknown) => {\n if (!Array.isArray(a) || !Array.isArray(b)) return false;\n if (a.length !== b.length) return false;\n const sortedA = [...a].sort();\n const sortedB = [...b].sort();\n return sortedA.every((val, i) => val === sortedB[i]);\n };\n\n // Check all objects\n const allSame = objects.every(obj => {\n const val = obj[prop];\n if (Array.isArray(firstValue) && Array.isArray(val)) {\n return arraysEqual(firstValue, val);\n }\n return val === firstValue;\n });\n\n if (allSame) {\n if (prop === 'description' && typeof firstValue === 'string') {\n schema.description = firstValue;\n } else if (prop === 'examples' && Array.isArray(firstValue)) {\n schema.examples = firstValue;\n } else if (prop === 'format' && typeof firstValue === 'string') {\n schema.format = firstValue;\n }\n }\n }\n }\n }\n}\n\nfunction transformValue(value?: Type | object | unknown) {\n let r: unknown = value;\n if (value && typeof value === 'function' && 'toJsonSchema' in value && typeof value.toJsonSchema === 'function') {\n r = (value as Type).toJsonSchema({\n fallback: (v) => {\n let r: JsonSchema & { _instanceof?: string } = {};\n let _instanceof = '';\n if (v && 'proto' in v && v.proto && typeof v.proto === 'function' && 'name' in v.proto) {\n r.type = 'object';\n r._instanceof = `${v.proto.name}`;\n _instanceof = `${v.proto.name}`;\n }\n if (v.base) {\n r = { ...r, ...v.base };\n if ('out' in v && v.out) {\n if ('anyOf' in r && 'type' in v.out) {\n const description = r.anyOf[0]?.description;\n r = { ...v.out };\n if (description) {\n r.description = description;\n }\n if (_instanceof) {\n r._instanceof = _instanceof;\n }\n } else {\n r = { ...r, ...v.out };\n }\n }\n }\n\n if ('properties' in r) {\n // because default values are not always in the schema (arktype bug)\n const r2 = (value as Type).toJSON()\n if ('in' in r2 &&\n r2.in &&\n typeof r2.in === 'object' &&\n 'optional' in r2.in) {\n if (Array.isArray(r2.in.optional)) {\n for (const prop of r2.in.optional) {\n if (typeof prop === 'object' &&\n prop &&\n 'key' in prop &&\n typeof prop.key === 'string' &&\n typeof r.properties[prop.key].default === 'undefined' &&\n 'default' in prop &&\n typeof prop.default !== 'undefined'\n ) {\n r.properties[prop.key].default = prop.default\n }\n }\n }\n }\n\n // reformat anyOf schemas\n for (const prop in r.properties) {\n reformatAnyOf(r.properties[prop])\n }\n }\n return r;\n },\n });\n }\n\n return r;\n}\n\nexport class OpenAPIArkHelper extends OpenAPIJsonHelper implements KaapiOpenAPIHelperInterface {\n protected _originalSchema?: Type\n constructor(\n params: {\n value?: Type | object | unknown;\n isRoot?: boolean;\n },\n isRequired?: boolean\n ) {\n super({ ...params, value: transformValue(params.value) }, isRequired);\n if (params.value instanceof Type) {\n this._originalSchema = params.value\n }\n }\n isValid(): boolean {\n return !!(\n this._schema &&\n typeof this._schema === 'object' &&\n !('~standard' in this._schema) &&\n (('type' in this._schema && typeof this._schema.type === 'string') ||\n ('oneOf' in this._schema && Array.isArray(this._schema.oneOf)) ||\n ('anyOf' in this._schema && Array.isArray(this._schema.anyOf)) ||\n ('enum' in this._schema && Array.isArray(this._schema.enum)))\n );\n }\n getFirstItem(): OpenAPIArkHelper | undefined {\n const schema = this._schema;\n\n if ('items' in schema && typeof schema.items === 'object') {\n return new OpenAPIArkHelper({ value: schema.items });\n }\n\n return;\n }\n getChildren(): Record<string, OpenAPIArkHelper> {\n const r: Record<string, OpenAPIArkHelper> = {};\n const schema = this._schema;\n if ('properties' in schema && typeof schema.properties === 'object' && schema.properties) {\n const properties: Record<string, unknown> = schema.properties as Record<string, unknown>;\n for (const p in properties) {\n const isRequired: boolean =\n 'required' in schema && Array.isArray(schema.required) && schema.required.includes(p);\n r[p] = new OpenAPIArkHelper({ value: properties[p] }, isRequired);\n }\n }\n return r;\n }\n getAlternatives(): OpenAPIArkHelper[] {\n const r: OpenAPIArkHelper[] = [];\n const schema = this._schema;\n if ('oneOf' in schema && Array.isArray(schema.oneOf)) {\n for (const p of schema.oneOf) {\n r.push(new OpenAPIArkHelper({ value: p }));\n }\n }\n return r;\n }\n isFile(): boolean | undefined {\n if (!this.isValid()) return false;\n let r: boolean = false;\n const schema = this._schema;\n if ('properties' in schema && typeof schema.properties === 'object' && schema.properties) {\n const properties: Record<string, unknown> = schema.properties as Record<string, unknown>;\n r = !!(\n '_data' in properties &&\n properties._data &&\n typeof properties._data === 'object' &&\n '_instanceof' in properties._data &&\n properties._data._instanceof === 'Buffer'\n );\n }\n return r;\n }\n isRequired(): boolean {\n let r = super.isRequired()\n if (!r && this._originalSchema) {\n const schema = this._schema;\n // if there is at least one required property\n if ('required' in schema &&\n Array.isArray(schema.required) &&\n 'properties' in schema &&\n typeof schema.properties === 'object' &&\n schema.properties &&\n schema.required.length <= Object.keys(schema.properties).length\n ) {\n r = true\n }\n }\n return r;\n }\n getFilesChildren(): Record<string, unknown> {\n const r: Record<string, unknown> = {};\n const schema = this._schema;\n if ('properties' in schema && typeof schema.properties === 'object' && schema.properties) {\n const properties: Record<string, unknown> = schema.properties as Record<string, unknown>;\n if (this._originalSchema) {\n const betterR: Record<string, Type> = {}\n for (const p in properties) {\n const isRequired: boolean =\n 'required' in schema && Array.isArray(schema.required) && schema.required.includes(p);\n const ch = new OpenAPIArkHelper({ value: properties[p] }, isRequired);\n if (ch.isFile()) {\n const propOriginalSchema = (this._originalSchema as Type<Record<string, unknown>>)?.props?.find(v => v.key === p)\n if (propOriginalSchema) {\n let key = p;\n if (!isRequired) {\n key = `${p}?` // set it as optional\n }\n betterR[key] = propOriginalSchema.value\n }\n }\n }\n if (Object.keys(betterR).length) {\n return type<unknown, Type<typeof betterR>>(betterR) as unknown as Record<string, unknown>\n }\n } else {\n for (const p in properties) {\n const isRequired: boolean =\n 'required' in schema && Array.isArray(schema.required) && schema.required.includes(p);\n const ch = new OpenAPIArkHelper({ value: properties[p] }, isRequired);\n if (ch.isFile()) {\n r[p] = properties[p];\n }\n }\n }\n }\n return r;\n }\n}\n\nexport class PostmanArkHelper extends PostmanJsonHelper {\n protected _originalSchema?: Type\n constructor(\n params: {\n value?: Type | object | unknown;\n isRoot?: boolean;\n },\n isRequired?: boolean\n ) {\n super({ ...params, value: transformValue(params.value) }, isRequired);\n if (params.value instanceof Type) {\n this._originalSchema = params.value\n }\n }\n isValid(): boolean {\n return !!(\n this._schema &&\n typeof this._schema === 'object' &&\n !('~standard' in this._schema) &&\n (('type' in this._schema && typeof this._schema.type === 'string') ||\n ('oneOf' in this._schema && Array.isArray(this._schema.oneOf)) ||\n ('anyOf' in this._schema && Array.isArray(this._schema.anyOf)) ||\n ('enum' in this._schema && Array.isArray(this._schema.enum)))\n );\n }\n isRequired(): boolean {\n let r = super.isRequired()\n if (!r && this._originalSchema) {\n const schema = this._schema;\n // if there is at least one required property\n if ('required' in schema &&\n Array.isArray(schema.required) &&\n 'properties' in schema &&\n typeof schema.properties === 'object' &&\n schema.properties &&\n schema.required.length <= Object.keys(schema.properties).length\n ) {\n r = true\n }\n }\n return r;\n }\n getFirstItem(): PostmanArkHelper | undefined {\n const schema = this._schema;\n\n if ('items' in schema && typeof schema.items === 'object') {\n return new PostmanArkHelper({ value: schema.items });\n }\n\n return;\n }\n getChildren(): Record<string, PostmanArkHelper> {\n const r: Record<string, PostmanArkHelper> = {};\n const schema = this._schema;\n if ('properties' in schema && typeof schema.properties === 'object' && schema.properties) {\n const properties: Record<string, unknown> = schema.properties as Record<string, unknown>;\n for (const p in properties) {\n const isRequired: boolean =\n 'required' in schema && Array.isArray(schema.required) && schema.required.includes(p);\n r[p] = new PostmanArkHelper({ value: properties[p] }, isRequired);\n }\n }\n return r;\n }\n getAlternatives(): PostmanArkHelper[] {\n const r: PostmanArkHelper[] = [];\n const schema = this._schema;\n if ('oneOf' in schema && Array.isArray(schema.oneOf)) {\n for (const p of schema.oneOf) {\n r.push(new PostmanArkHelper({ value: p }));\n }\n }\n return r;\n }\n}\n"],"mappings":";AACA,SAAS,mBAAmB,yBAAyB;AACrD,SAAS,MAAM,YAA6B;AAQ5C,SAAS,cAAc,QAAoB;AACvC,MAAI,WAAW,QAAQ;AACnB,UAAM,oBAA+D,CAAC;AACtE,QAAI,CAAC,OAAO,aAAa;AACrB,wBAAkB,KAAK,aAAa;AAAA,IACxC;AACA,QAAI,CAAC,OAAO,UAAU;AAClB,wBAAkB,KAAK,UAAU;AAAA,IACrC;AACA,QAAI,CAAC,OAAO,QAAQ;AAChB,wBAAkB,KAAK,QAAQ;AAAA,IACnC;AACA,UAAM,UAAU,OAAO;AACvB,eAAW,QAAQ,mBAAmB;AAElC,YAAM,eAAe,QAAQ,CAAC;AAC9B,UAAI,gBAAgB,OAAO,iBAAiB,UAAU;AAClD,cAAM,aAAa,aAAa,IAAI;AAGpC,cAAM,cAAc,CAAC,GAAY,MAAe;AAC5C,cAAI,CAAC,MAAM,QAAQ,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,EAAG,QAAO;AACn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{
|
|
8
8
|
name: "@kaapi/validator-arktype",
|
|
9
9
|
type: "module",
|
|
10
|
-
version: "0.0.
|
|
10
|
+
version: "0.0.34",
|
|
11
11
|
private: false,
|
|
12
12
|
description: "ArkType-powered request validation and documentation plugin for Kaapi.",
|
|
13
13
|
main: "lib/index.js",
|
|
@@ -47,7 +47,7 @@ var package_default = {
|
|
|
47
47
|
dependencies: {
|
|
48
48
|
"@hapi/boom": "^10.0.1",
|
|
49
49
|
"@kaapi/kaapi": "workspace:^",
|
|
50
|
-
"@novice1/api-doc-json-helper": "^1.0.
|
|
50
|
+
"@novice1/api-doc-json-helper": "^1.0.8",
|
|
51
51
|
arktype: "^2.1.25",
|
|
52
52
|
tslib: "^2.8.1"
|
|
53
53
|
},
|
|
@@ -189,4 +189,4 @@ export {
|
|
|
189
189
|
supportedProps,
|
|
190
190
|
validatorArk
|
|
191
191
|
};
|
|
192
|
-
//# sourceMappingURL=chunk-
|
|
192
|
+
//# sourceMappingURL=chunk-U7WK3Q5P.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../package.json","../src/validator.ts"],"sourcesContent":["{\n \"name\": \"@kaapi/validator-arktype\",\n \"type\": \"module\",\n \"version\": \"0.0.34\",\n \"private\": false,\n \"description\": \"ArkType-powered request validation and documentation plugin for Kaapi.\",\n \"main\": \"lib/index.js\",\n \"exports\": {\n \".\": {\n \"types\": \"./lib/index.d.ts\",\n \"default\": \"./lib/index.js\"\n }\n },\n \"scripts\": {\n \"build\": \"tsup src !src/**/*.d.ts --format esm --outDir lib --clean --metafile --dts --sourcemap && node ./scripts/buildDT.mjs\",\n \"check\": \"tsc --noEmit\",\n \"check:deps\": \"depcruise src --validate\",\n \"ci\": \"npm run verify\",\n \"coverage\": \"c8 node --loader ts-node/esm --experimental-specifier-resolution=node --no-warnings ../../node_modules/mocha/bin/mocha test/**/*.spec.ts\",\n \"format\": \"prettier --write ./\",\n \"format:check\": \"prettier --check ./\",\n \"lint\": \"eslint .\",\n \"test\": \"node --loader ts-node/esm --experimental-specifier-resolution=node --no-warnings ../../node_modules/mocha/bin/mocha test/**/*.spec.ts\",\n \"type-coverage\": \"type-coverage --at-least 90 --detail\",\n \"verify\": \"npm run lint && npm run format:check && npm run check && npm run check:deps && npm run type-coverage && npm run test:kaukau && npm run coverage\"\n },\n \"keywords\": [\n \"kaapi\",\n \"hapi\",\n \"arktype\",\n \"typescript\",\n \"validation\"\n ],\n \"author\": \"demingongo\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/demingongo/kaapi.git\",\n \"directory\": \"packages/validator-arktype\"\n },\n \"license\": \"MIT\",\n \"dependencies\": {\n \"@hapi/boom\": \"^10.0.1\",\n \"@kaapi/kaapi\": \"workspace:^\",\n \"@novice1/api-doc-json-helper\": \"^1.0.8\",\n \"arktype\": \"^2.1.25\",\n \"tslib\": \"^2.8.1\"\n },\n \"devDependencies\": {\n \"@types/mocha\": \"^10.0.10\"\n },\n \"peerDependencies\": {\n \"arktype\": \"^2.1.25\"\n }\n}","import pkg from '../package.json' with { type: 'json' };\nimport { OpenAPIArkHelper, PostmanArkHelper } from './doc-helpers.js';\nimport type {\n ArklessReqRef,\n ArklessReqRefDefaults,\n ValidatorArk,\n ValidatorArkReqRef,\n ValidatorArkSchema,\n} from './types.js';\nimport Boom from '@hapi/boom';\nimport type {\n KaapiServerRoute,\n HandlerDecorations,\n Lifecycle,\n KaapiPlugin,\n Request,\n ResponseToolkit,\n RouteOptions,\n} from '@kaapi/kaapi';\nimport { type, type Type } from 'arktype';\n\nconst { parse = { payload: true, query: true, params: true, headers: true, state: true } } = {};\nexport const supportedProps = ['payload', 'query', 'params', 'headers', 'state'] as const;\nconst normalizeBooleans = (obj: Record<string, unknown>) => {\n for (const key in obj) {\n const val = obj[key];\n if (typeof val === 'string') {\n if (val === 'true') obj[key] = true;\n else if (val === 'false') obj[key] = false;\n } else if (Array.isArray(val)) {\n obj[key] = val.map((v) => (v === 'true' ? true : v === 'false' ? false : v));\n }\n }\n return obj;\n};\n\nfunction mergeOptions<V extends ValidatorArkSchema, R extends ArklessReqRef>(\n options: RouteOptions<ValidatorArkReqRef<V> & R>,\n schema: V\n) {\n // validator\n if (!options.plugins) {\n options.plugins = {};\n }\n options.plugins.ark = schema;\n\n // docs\n options.plugins.kaapi = options.plugins.kaapi || {};\n if (\n options.plugins.kaapi.docs != false && // docs not disabled\n !options.plugins.kaapi.docs?.disabled // docs not disabled\n ) {\n if (!options.plugins?.kaapi?.docs?.helperSchemaProperty)\n // docs have not helperSchemaProperty\n options.plugins.kaapi.docs = {\n ...options.plugins.kaapi.docs,\n helperSchemaProperty: 'ark',\n };\n if (!options.plugins?.kaapi?.docs?.openAPIHelperClass)\n // docs have not openAPIHelperClass\n options.plugins.kaapi.docs = {\n ...options.plugins.kaapi.docs,\n openAPIHelperClass: OpenAPIArkHelper,\n };\n }\n return options\n}\n\nexport const validatorArk: KaapiPlugin = {\n async integrate(t) {\n const validator: ValidatorArk = <V extends ValidatorArkSchema>(schema: V) => {\n return {\n route<R extends ArklessReqRef = ArklessReqRefDefaults>(\n serverRoute: KaapiServerRoute<ValidatorArkReqRef<V> & R>,\n handler?:\n | HandlerDecorations\n | Lifecycle.Method<ValidatorArkReqRef<V> & R, Lifecycle.ReturnValue<ValidatorArkReqRef<V> & R>>\n ) {\n if (!serverRoute.options) {\n serverRoute.options = {};\n }\n if (typeof serverRoute.options === 'object') {\n mergeOptions(serverRoute.options, schema)\n } else if (typeof serverRoute.options === 'function') {\n const fn = serverRoute.options.bind(serverRoute);\n serverRoute.options = (server) => {\n const options = fn(server)\n return mergeOptions(options, schema)\n }\n }\n t.route(serverRoute, handler);\n return t.server;\n },\n };\n };\n\n await t.server.register({\n name: 'kaapi-validator-arktype',\n version: pkg.version,\n register: async function (server) {\n server.ext('onPreHandler', async (request: Request, h: ResponseToolkit) => {\n const routeValidation = request?.route?.settings?.plugins?.ark as ValidatorArkSchema | undefined;\n try {\n // Initialize empty objects to hold the parsed data and corresponding ArkType schemas\n const data: Record<string, unknown> = {};\n const dataSchema: Record<string, Type> = {};\n\n // Loop through all supported properties for this route\n for (const prop of supportedProps) {\n // Check if validation exists for this property and there is a parser defined\n if (routeValidation?.[prop] && parse[prop]) {\n // Add the ArkType schema for this property to the dataSchema\n dataSchema[prop] = routeValidation[prop];\n // Prepare the value for parsing:\n // - For query params, normalize boolean strings to actual booleans\n // - Otherwise, take the raw value from the request object\n data[prop] = prop === 'query' ? normalizeBooleans(request[prop]) : request[prop];\n }\n }\n\n // Determine if there are any properties to validate\n let hasProps = false;\n for (const key in dataSchema) {\n // Safely check own properties to avoid inherited keys\n if (Object.prototype.hasOwnProperty.call(dataSchema, key)) {\n hasProps = true;\n break;\n }\n }\n\n // If we have any props to validate, parse them using ArkType\n if (hasProps) {\n // Create an ArkType object from the collected schema and parse\n const parsedProps = type(dataSchema)(data);\n if (parsedProps instanceof type.errors) {\n // throw ArkErrors\n throw parsedProps;\n }\n // Merge the parsed and validated properties back into the request object\n Object.assign(request, parsedProps);\n }\n\n // Continue the Hapi request lifecycle\n return h.continue;\n } catch (err) {\n // Initialize a set to track which paths (properties) failed validation\n const issuePaths = new Set<string>();\n let message: string;\n\n // Check if the error is instance of ValiError\n if (err instanceof type.errors && err.issues.length) {\n const firstIssue = err.issues[0];\n message = firstIssue.message;\n } else if (err instanceof Error) {\n // If it’s a regular Error, use its message\n message = err.message;\n } else {\n // Unknown error type\n message = 'Unknown error';\n }\n\n // Create a Boom badRequest response with the error message\n const response = Boom.badRequest(message);\n\n // Attach the raw validation error object for debugging/logging\n response.data = {\n validationError: err,\n };\n\n // Handle custom failAction if it’s a function\n if (typeof routeValidation?.failAction === 'function') {\n return routeValidation.failAction(request, h, response);\n }\n\n // If failAction is 'log', log the validation error with the request\n if (routeValidation?.failAction === 'log') {\n request.log(['validation', 'error', 'arktype', ...issuePaths], response);\n // Note: unlike Hapi's failAction 'log', 'log' here still returns a Boom response\n }\n\n // Return the error response to halt request processing\n return response;\n }\n });\n server.decorate('server', 'ark', validator);\n },\n });\n\n if (t.openapi) {\n t.openapi.addHelperClass(OpenAPIArkHelper);\n }\n if (t.postman) {\n t.postman.addHelperClass(PostmanArkHelper);\n }\n },\n};\n"],"mappings":";;;;;;AAAA;AAAA,EACE,MAAQ;AAAA,EACR,MAAQ;AAAA,EACR,SAAW;AAAA,EACX,SAAW;AAAA,EACX,aAAe;AAAA,EACf,MAAQ;AAAA,EACR,SAAW;AAAA,IACT,KAAK;AAAA,MACH,OAAS;AAAA,MACT,SAAW;AAAA,IACb;AAAA,EACF;AAAA,EACA,SAAW;AAAA,IACT,OAAS;AAAA,IACT,OAAS;AAAA,IACT,cAAc;AAAA,IACd,IAAM;AAAA,IACN,UAAY;AAAA,IACZ,QAAU;AAAA,IACV,gBAAgB;AAAA,IAChB,MAAQ;AAAA,IACR,MAAQ;AAAA,IACR,iBAAiB;AAAA,IACjB,QAAU;AAAA,EACZ;AAAA,EACA,UAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAAA,EACA,QAAU;AAAA,EACV,YAAc;AAAA,IACZ,MAAQ;AAAA,IACR,KAAO;AAAA,IACP,WAAa;AAAA,EACf;AAAA,EACA,SAAW;AAAA,EACX,cAAgB;AAAA,IACd,cAAc;AAAA,IACd,gBAAgB;AAAA,IAChB,gCAAgC;AAAA,IAChC,SAAW;AAAA,IACX,OAAS;AAAA,EACX;AAAA,EACA,iBAAmB;AAAA,IACjB,gBAAgB;AAAA,EAClB;AAAA,EACA,kBAAoB;AAAA,IAClB,SAAW;AAAA,EACb;AACF;;;AC5CA,OAAO,UAAU;AAUjB,SAAS,YAAuB;AAEhC,IAAM,EAAE,QAAQ,EAAE,SAAS,MAAM,OAAO,MAAM,QAAQ,MAAM,SAAS,MAAM,OAAO,KAAK,EAAE,IAAI,CAAC;AACvF,IAAM,iBAAiB,CAAC,WAAW,SAAS,UAAU,WAAW,OAAO;AAC/E,IAAM,oBAAo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AAI,OAAO,CAAC;AAC/B,wBAAU,WAAW;AAAA,YACzB,WAAW,eAAe,OAAO;AAE7B,wBAAU,IAAI;AAAA,YAClB,OAAO;AAEH,wBAAU;AAAA,YACd;AAGA,kBAAM,WAAW,KAAK,WAAW,OAAO;AAGxC,qBAAS,OAAO;AAAA,cACZ,iBAAiB;AAAA,YACrB;AAGA,gBAAI,OAAO,iBAAiB,eAAe,YAAY;AACnD,qBAAO,gBAAgB,WAAW,SAAS,GAAG,QAAQ;AAAA,YAC1D;AAGA,gBAAI,iBAAiB,eAAe,OAAO;AACvC,sBAAQ,IAAI,CAAC,cAAc,SAAS,WAAW,GAAG,UAAU,GAAG,QAAQ;AAAA,YAE3E;AAGA,mBAAO;AAAA,UACX;AAAA,QACJ,CAAC;AACD,eAAO,SAAS,UAAU,OAAO,SAAS;AAAA,MAC9C;AAAA,IACJ,CAAC;AAED,QAAI,EAAE,SAAS;AACX,QAAE,QAAQ,eAAe,gBAAgB;AAAA,IAC7C;AACA,QAAI,EAAE,SAAS;AACX,QAAE,QAAQ,eAAe,gBAAgB;AAAA,IAC7C;AAAA,EACJ;AACJ;","names":[]}
|
package/lib/doc-helpers.d.ts
CHANGED
|
@@ -17,11 +17,13 @@ declare class OpenAPIArkHelper extends OpenAPIJsonHelper implements KaapiOpenAPI
|
|
|
17
17
|
getFilesChildren(): Record<string, unknown>;
|
|
18
18
|
}
|
|
19
19
|
declare class PostmanArkHelper extends PostmanJsonHelper {
|
|
20
|
+
protected _originalSchema?: Type;
|
|
20
21
|
constructor(params: {
|
|
21
22
|
value?: Type | object | unknown;
|
|
22
23
|
isRoot?: boolean;
|
|
23
24
|
}, isRequired?: boolean);
|
|
24
25
|
isValid(): boolean;
|
|
26
|
+
isRequired(): boolean;
|
|
25
27
|
getFirstItem(): PostmanArkHelper | undefined;
|
|
26
28
|
getChildren(): Record<string, PostmanArkHelper>;
|
|
27
29
|
getAlternatives(): PostmanArkHelper[];
|
package/lib/doc-helpers.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -2,11 +2,11 @@ import "./chunk-55J6XMHW.js";
|
|
|
2
2
|
import {
|
|
3
3
|
supportedProps,
|
|
4
4
|
validatorArk
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-U7WK3Q5P.js";
|
|
6
6
|
import {
|
|
7
7
|
OpenAPIArkHelper,
|
|
8
8
|
PostmanArkHelper
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-QLKH743P.js";
|
|
10
10
|
export {
|
|
11
11
|
OpenAPIArkHelper,
|
|
12
12
|
PostmanArkHelper,
|
package/lib/metafile-esm.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/doc-helpers.ts":{"bytes":
|
|
1
|
+
{"inputs":{"src/doc-helpers.ts":{"bytes":13491,"imports":[{"path":"@novice1/api-doc-json-helper","kind":"import-statement","external":true},{"path":"arktype","kind":"import-statement","external":true}],"format":"esm"},"src/types.ts":{"bytes":1833,"imports":[],"format":"esm"},"package.json":{"bytes":1796,"imports":[],"format":"esm","with":{"type":"json"}},"src/validator.ts":{"bytes":8672,"imports":[{"path":"package.json","kind":"import-statement","original":"../package.json","with":{"type":"json"}},{"path":"src/doc-helpers.ts","kind":"import-statement","original":"./doc-helpers.js"},{"path":"@hapi/boom","kind":"import-statement","external":true},{"path":"arktype","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":94,"imports":[{"path":"src/types.ts","kind":"import-statement","original":"./types.js"},{"path":"src/doc-helpers.ts","kind":"import-statement","original":"./doc-helpers.js"},{"path":"src/validator.ts","kind":"import-statement","original":"./validator.js"}],"format":"esm"}},"outputs":{"lib/doc-helpers.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":93},"lib/doc-helpers.js":{"imports":[{"path":"lib/chunk-QLKH743P.js","kind":"import-statement"}],"exports":["OpenAPIArkHelper","PostmanArkHelper"],"entryPoint":"src/doc-helpers.ts","inputs":{},"bytes":129},"lib/index.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":93},"lib/index.js":{"imports":[{"path":"lib/chunk-55J6XMHW.js","kind":"import-statement"},{"path":"lib/chunk-U7WK3Q5P.js","kind":"import-statement"},{"path":"lib/chunk-QLKH743P.js","kind":"import-statement"}],"exports":["OpenAPIArkHelper","PostmanArkHelper","supportedProps","validatorArk"],"entryPoint":"src/index.ts","inputs":{"src/index.ts":{"bytesInOutput":0}},"bytes":265},"lib/types.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":93},"lib/types.js":{"imports":[{"path":"lib/chunk-55J6XMHW.js","kind":"import-statement"}],"exports":[],"entryPoint":"src/types.ts","inputs":{},"bytes":30},"lib/chunk-55J6XMHW.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":93},"lib/chunk-55J6XMHW.js":{"imports":[],"exports":[],"inputs":{"src/types.ts":{"bytesInOutput":0}},"bytes":0},"lib/validator.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":93},"lib/validator.js":{"imports":[{"path":"lib/chunk-U7WK3Q5P.js","kind":"import-statement"},{"path":"lib/chunk-QLKH743P.js","kind":"import-statement"}],"exports":["supportedProps","validatorArk"],"entryPoint":"src/validator.ts","inputs":{},"bytes":147},"lib/chunk-U7WK3Q5P.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":14608},"lib/chunk-U7WK3Q5P.js":{"imports":[{"path":"lib/chunk-QLKH743P.js","kind":"import-statement"},{"path":"@hapi/boom","kind":"import-statement","external":true},{"path":"arktype","kind":"import-statement","external":true}],"exports":["supportedProps","validatorArk"],"inputs":{"package.json":{"bytesInOutput":1758},"src/validator.ts":{"bytesInOutput":4350}},"bytes":6270},"lib/chunk-QLKH743P.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":20569},"lib/chunk-QLKH743P.js":{"imports":[{"path":"@novice1/api-doc-json-helper","kind":"import-statement","external":true},{"path":"arktype","kind":"import-statement","external":true}],"exports":["OpenAPIArkHelper","PostmanArkHelper"],"inputs":{"src/doc-helpers.ts":{"bytesInOutput":9196}},"bytes":9270}}}
|
package/lib/validator.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaapi/validator-arktype",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.34",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "ArkType-powered request validation and documentation plugin for Kaapi.",
|
|
7
7
|
"main": "lib/index.js",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@hapi/boom": "^10.0.1",
|
|
30
|
-
"@novice1/api-doc-json-helper": "^1.0.
|
|
30
|
+
"@novice1/api-doc-json-helper": "^1.0.8",
|
|
31
31
|
"arktype": "^2.1.25",
|
|
32
32
|
"tslib": "^2.8.1",
|
|
33
|
-
"@kaapi/kaapi": "^0.0.
|
|
33
|
+
"@kaapi/kaapi": "^0.0.34"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/mocha": "^10.0.10"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../package.json","../src/validator.ts"],"sourcesContent":["{\n \"name\": \"@kaapi/validator-arktype\",\n \"type\": \"module\",\n \"version\": \"0.0.33\",\n \"private\": false,\n \"description\": \"ArkType-powered request validation and documentation plugin for Kaapi.\",\n \"main\": \"lib/index.js\",\n \"exports\": {\n \".\": {\n \"types\": \"./lib/index.d.ts\",\n \"default\": \"./lib/index.js\"\n }\n },\n \"scripts\": {\n \"build\": \"tsup src !src/**/*.d.ts --format esm --outDir lib --clean --metafile --dts --sourcemap && node ./scripts/buildDT.mjs\",\n \"check\": \"tsc --noEmit\",\n \"check:deps\": \"depcruise src --validate\",\n \"ci\": \"npm run verify\",\n \"coverage\": \"c8 node --loader ts-node/esm --experimental-specifier-resolution=node --no-warnings ../../node_modules/mocha/bin/mocha test/**/*.spec.ts\",\n \"format\": \"prettier --write ./\",\n \"format:check\": \"prettier --check ./\",\n \"lint\": \"eslint .\",\n \"test\": \"node --loader ts-node/esm --experimental-specifier-resolution=node --no-warnings ../../node_modules/mocha/bin/mocha test/**/*.spec.ts\",\n \"type-coverage\": \"type-coverage --at-least 90 --detail\",\n \"verify\": \"npm run lint && npm run format:check && npm run check && npm run check:deps && npm run type-coverage && npm run test:kaukau && npm run coverage\"\n },\n \"keywords\": [\n \"kaapi\",\n \"hapi\",\n \"arktype\",\n \"typescript\",\n \"validation\"\n ],\n \"author\": \"demingongo\",\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/demingongo/kaapi.git\",\n \"directory\": \"packages/validator-arktype\"\n },\n \"license\": \"MIT\",\n \"dependencies\": {\n \"@hapi/boom\": \"^10.0.1\",\n \"@kaapi/kaapi\": \"workspace:^\",\n \"@novice1/api-doc-json-helper\": \"^1.0.6\",\n \"arktype\": \"^2.1.25\",\n \"tslib\": \"^2.8.1\"\n },\n \"devDependencies\": {\n \"@types/mocha\": \"^10.0.10\"\n },\n \"peerDependencies\": {\n \"arktype\": \"^2.1.25\"\n }\n}\n","import pkg from '../package.json' with { type: 'json' };\nimport { OpenAPIArkHelper, PostmanArkHelper } from './doc-helpers.js';\nimport type {\n ArklessReqRef,\n ArklessReqRefDefaults,\n ValidatorArk,\n ValidatorArkReqRef,\n ValidatorArkSchema,\n} from './types.js';\nimport Boom from '@hapi/boom';\nimport type {\n KaapiServerRoute,\n HandlerDecorations,\n Lifecycle,\n KaapiPlugin,\n Request,\n ResponseToolkit,\n RouteOptions,\n} from '@kaapi/kaapi';\nimport { type, type Type } from 'arktype';\n\nconst { parse = { payload: true, query: true, params: true, headers: true, state: true } } = {};\nexport const supportedProps = ['payload', 'query', 'params', 'headers', 'state'] as const;\nconst normalizeBooleans = (obj: Record<string, unknown>) => {\n for (const key in obj) {\n const val = obj[key];\n if (typeof val === 'string') {\n if (val === 'true') obj[key] = true;\n else if (val === 'false') obj[key] = false;\n } else if (Array.isArray(val)) {\n obj[key] = val.map((v) => (v === 'true' ? true : v === 'false' ? false : v));\n }\n }\n return obj;\n};\n\nfunction mergeOptions<V extends ValidatorArkSchema, R extends ArklessReqRef>(\n options: RouteOptions<ValidatorArkReqRef<V> & R>,\n schema: V\n) {\n // validator\n if (!options.plugins) {\n options.plugins = {};\n }\n options.plugins.ark = schema;\n\n // docs\n options.plugins.kaapi = options.plugins.kaapi || {};\n if (\n options.plugins.kaapi.docs != false && // docs not disabled\n !options.plugins.kaapi.docs?.disabled // docs not disabled\n ) {\n if (!options.plugins?.kaapi?.docs?.helperSchemaProperty)\n // docs have not helperSchemaProperty\n options.plugins.kaapi.docs = {\n ...options.plugins.kaapi.docs,\n helperSchemaProperty: 'ark',\n };\n if (!options.plugins?.kaapi?.docs?.openAPIHelperClass)\n // docs have not openAPIHelperClass\n options.plugins.kaapi.docs = {\n ...options.plugins.kaapi.docs,\n openAPIHelperClass: OpenAPIArkHelper,\n };\n }\n return options\n}\n\nexport const validatorArk: KaapiPlugin = {\n async integrate(t) {\n const validator: ValidatorArk = <V extends ValidatorArkSchema>(schema: V) => {\n return {\n route<R extends ArklessReqRef = ArklessReqRefDefaults>(\n serverRoute: KaapiServerRoute<ValidatorArkReqRef<V> & R>,\n handler?:\n | HandlerDecorations\n | Lifecycle.Method<ValidatorArkReqRef<V> & R, Lifecycle.ReturnValue<ValidatorArkReqRef<V> & R>>\n ) {\n if (!serverRoute.options) {\n serverRoute.options = {};\n }\n if (typeof serverRoute.options === 'object') {\n mergeOptions(serverRoute.options, schema)\n } else if (typeof serverRoute.options === 'function') {\n const fn = serverRoute.options.bind(serverRoute);\n serverRoute.options = (server) => {\n const options = fn(server)\n return mergeOptions(options, schema)\n }\n }\n t.route(serverRoute, handler);\n return t.server;\n },\n };\n };\n\n await t.server.register({\n name: 'kaapi-validator-arktype',\n version: pkg.version,\n register: async function (server) {\n server.ext('onPreHandler', async (request: Request, h: ResponseToolkit) => {\n const routeValidation = request?.route?.settings?.plugins?.ark as ValidatorArkSchema | undefined;\n try {\n // Initialize empty objects to hold the parsed data and corresponding ArkType schemas\n const data: Record<string, unknown> = {};\n const dataSchema: Record<string, Type> = {};\n\n // Loop through all supported properties for this route\n for (const prop of supportedProps) {\n // Check if validation exists for this property and there is a parser defined\n if (routeValidation?.[prop] && parse[prop]) {\n // Add the ArkType schema for this property to the dataSchema\n dataSchema[prop] = routeValidation[prop];\n // Prepare the value for parsing:\n // - For query params, normalize boolean strings to actual booleans\n // - Otherwise, take the raw value from the request object\n data[prop] = prop === 'query' ? normalizeBooleans(request[prop]) : request[prop];\n }\n }\n\n // Determine if there are any properties to validate\n let hasProps = false;\n for (const key in dataSchema) {\n // Safely check own properties to avoid inherited keys\n if (Object.prototype.hasOwnProperty.call(dataSchema, key)) {\n hasProps = true;\n break;\n }\n }\n\n // If we have any props to validate, parse them using ArkType\n if (hasProps) {\n // Create an ArkType object from the collected schema and parse\n const parsedProps = type(dataSchema)(data);\n if (parsedProps instanceof type.errors) {\n // throw ArkErrors\n throw parsedProps;\n }\n // Merge the parsed and validated properties back into the request object\n Object.assign(request, parsedProps);\n }\n\n // Continue the Hapi request lifecycle\n return h.continue;\n } catch (err) {\n // Initialize a set to track which paths (properties) failed validation\n const issuePaths = new Set<string>();\n let message: string;\n\n // Check if the error is instance of ValiError\n if (err instanceof type.errors && err.issues.length) {\n const firstIssue = err.issues[0];\n message = firstIssue.message;\n } else if (err instanceof Error) {\n // If it’s a regular Error, use its message\n message = err.message;\n } else {\n // Unknown error type\n message = 'Unknown error';\n }\n\n // Create a Boom badRequest response with the error message\n const response = Boom.badRequest(message);\n\n // Attach the raw validation error object for debugging/logging\n response.data = {\n validationError: err,\n };\n\n // Handle custom failAction if it’s a function\n if (typeof routeValidation?.failAction === 'function') {\n return routeValidation.failAction(request, h, response);\n }\n\n // If failAction is 'log', log the validation error with the request\n if (routeValidation?.failAction === 'log') {\n request.log(['validation', 'error', 'arktype', ...issuePaths], response);\n // Note: unlike Hapi's failAction 'log', 'log' here still returns a Boom response\n }\n\n // Return the error response to halt request processing\n return response;\n }\n });\n server.decorate('server', 'ark', validator);\n },\n });\n\n if (t.openapi) {\n t.openapi.addHelperClass(OpenAPIArkHelper);\n }\n if (t.postman) {\n t.postman.addHelperClass(PostmanArkHelper);\n }\n },\n};\n"],"mappings":";;;;;;AAAA;AAAA,EACE,MAAQ;AAAA,EACR,MAAQ;AAAA,EACR,SAAW;AAAA,EACX,SAAW;AAAA,EACX,aAAe;AAAA,EACf,MAAQ;AAAA,EACR,SAAW;AAAA,IACT,KAAK;AAAA,MACH,OAAS;AAAA,MACT,SAAW;AAAA,IACb;AAAA,EACF;AAAA,EACA,SAAW;AAAA,IACT,OAAS;AAAA,IACT,OAAS;AAAA,IACT,cAAc;AAAA,IACd,IAAM;AAAA,IACN,UAAY;AAAA,IACZ,QAAU;AAAA,IACV,gBAAgB;AAAA,IAChB,MAAQ;AAAA,IACR,MAAQ;AAAA,IACR,iBAAiB;AAAA,IACjB,QAAU;AAAA,EACZ;AAAA,EACA,UAAY;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAAA,EACA,QAAU;AAAA,EACV,YAAc;AAAA,IACZ,MAAQ;AAAA,IACR,KAAO;AAAA,IACP,WAAa;AAAA,EACf;AAAA,EACA,SAAW;AAAA,EACX,cAAgB;AAAA,IACd,cAAc;AAAA,IACd,gBAAgB;AAAA,IAChB,gCAAgC;AAAA,IAChC,SAAW;AAAA,IACX,OAAS;AAAA,EACX;AAAA,EACA,iBAAmB;AAAA,IACjB,gBAAgB;AAAA,EAClB;AAAA,EACA,kBAAoB;AAAA,IAClB,SAAW;AAAA,EACb;AACF;;;AC5CA,OAAO,UAAU;AAUjB,SAAS,YAAuB;AAEhC,IAAM,EAAE,QAAQ,EAAE,SAAS,MAAM,OAAO,MAAM,QAAQ,MAAM,SAAS,MAAM,OAAO,KAAK,EAAE,IAAI,CAAC;AACvF,IAAM,iBAAiB,C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e,KAAK,UAAU,IAAI,OAAO,QAAQ;AACjD,oBAAM,aAAa,IAAI,OAAO,CAAC;AAC/B,wBAAU,WAAW;AAAA,YACzB,WAAW,eAAe,OAAO;AAE7B,wBAAU,IAAI;AAAA,YAClB,OAAO;AAEH,wBAAU;AAAA,YACd;AAGA,kBAAM,WAAW,KAAK,WAAW,OAAO;AAGxC,qBAAS,OAAO;AAAA,cACZ,iBAAiB;AAAA,YACrB;AAGA,gBAAI,OAAO,iBAAiB,eAAe,YAAY;AACnD,qBAAO,gBAAgB,WAAW,SAAS,GAAG,QAAQ;AAAA,YAC1D;AAGA,gBAAI,iBAAiB,eAAe,OAAO;AACvC,sBAAQ,IAAI,CAAC,cAAc,SAAS,WAAW,GAAG,UAAU,GAAG,QAAQ;AAAA,YAE3E;AAGA,mBAAO;AAAA,UACX;AAAA,QACJ,CAAC;AACD,eAAO,SAAS,UAAU,OAAO,SAAS;AAAA,MAC9C;AAAA,IACJ,CAAC;AAED,QAAI,EAAE,SAAS;AACX,QAAE,QAAQ,eAAe,gBAAgB;AAAA,IAC7C;AACA,QAAI,EAAE,SAAS;AACX,QAAE,QAAQ,eAAe,gBAAgB;AAAA,IAC7C;AAAA,EACJ;AACJ;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/doc-helpers.ts"],"sourcesContent":["import type { KaapiOpenAPIHelperInterface } from '@kaapi/kaapi';\nimport { OpenAPIJsonHelper, PostmanJsonHelper } from '@novice1/api-doc-json-helper';\nimport { type, Type, type JsonSchema } from 'arktype';\n\nfunction transformValue(value?: Type | object | unknown) {\n let r: unknown = value;\n if (value && typeof value === 'function' && 'toJsonSchema' in value && typeof value.toJsonSchema === 'function') {\n r = (value as Type).toJsonSchema({\n fallback: (v) => {\n let r: JsonSchema & { _instanceof?: string } = {};\n let _instanceof = '';\n if (v && 'proto' in v && v.proto && typeof v.proto === 'function' && 'name' in v.proto) {\n r.type = 'object';\n r._instanceof = `${v.proto.name}`;\n _instanceof = `${v.proto.name}`;\n }\n if (v.base) {\n r = { ...r, ...v.base };\n if ('out' in v && v.out) {\n if ('anyOf' in r && 'type' in v.out) {\n const description = r.anyOf[0]?.description;\n r = { ...v.out };\n if (description) {\n r.description = description;\n }\n if (_instanceof) {\n r._instanceof = _instanceof;\n }\n } else {\n r = { ...r, ...v.out };\n }\n }\n }\n return r;\n },\n });\n }\n\n return r;\n}\n\nexport class OpenAPIArkHelper extends OpenAPIJsonHelper implements KaapiOpenAPIHelperInterface {\n protected _originalSchema?: Type\n constructor(\n params: {\n value?: Type | object | unknown;\n isRoot?: boolean;\n },\n isRequired?: boolean\n ) {\n super({ ...params, value: transformValue(params.value) }, isRequired);\n if (params.value instanceof Type) {\n this._originalSchema = params.value\n }\n }\n isValid(): boolean {\n return !!(\n this._schema &&\n typeof this._schema === 'object' &&\n !('~standard' in this._schema) &&\n (('type' in this._schema && typeof this._schema.type === 'string') ||\n ('oneOf' in this._schema && Array.isArray(this._schema.oneOf)) ||\n ('anyOf' in this._schema && Array.isArray(this._schema.anyOf)) ||\n ('enum' in this._schema && Array.isArray(this._schema.enum)))\n );\n }\n getFirstItem(): OpenAPIArkHelper | undefined {\n const schema = this._schema;\n\n if ('items' in schema && typeof schema.items === 'object') {\n return new OpenAPIArkHelper({ value: schema.items });\n }\n\n return;\n }\n getChildren(): Record<string, OpenAPIArkHelper> {\n const r: Record<string, OpenAPIArkHelper> = {};\n const schema = this._schema;\n if ('properties' in schema && typeof schema.properties === 'object' && schema.properties) {\n const properties: Record<string, unknown> = schema.properties as Record<string, unknown>;\n for (const p in properties) {\n const isRequired: boolean =\n 'required' in schema && Array.isArray(schema.required) && schema.required.includes(p);\n r[p] = new OpenAPIArkHelper({ value: properties[p] }, isRequired);\n }\n }\n return r;\n }\n getAlternatives(): OpenAPIArkHelper[] {\n const r: OpenAPIArkHelper[] = [];\n const schema = this._schema;\n if ('oneOf' in schema && Array.isArray(schema.oneOf)) {\n for (const p of schema.oneOf) {\n r.push(new OpenAPIArkHelper({ value: p }));\n }\n }\n return r;\n }\n isFile(): boolean | undefined {\n if (!this.isValid()) return false;\n let r: boolean = false;\n const schema = this._schema;\n if ('properties' in schema && typeof schema.properties === 'object' && schema.properties) {\n const properties: Record<string, unknown> = schema.properties as Record<string, unknown>;\n r = !!(\n '_data' in properties &&\n properties._data &&\n typeof properties._data === 'object' &&\n '_instanceof' in properties._data &&\n properties._data._instanceof === 'Buffer'\n );\n }\n return r;\n }\n isRequired(): boolean {\n let r = super.isRequired()\n if (!r && this._originalSchema) {\n const schema = this._schema;\n if ('required' in schema &&\n Array.isArray(schema.required) &&\n 'properties' in schema &&\n typeof schema.properties === 'object' &&\n schema.properties &&\n schema.required.length === Object.keys(schema.properties).length\n ) {\n r = true\n }\n }\n return r;\n }\n getFilesChildren(): Record<string, unknown> {\n const r: Record<string, unknown> = {};\n const schema = this._schema;\n if ('properties' in schema && typeof schema.properties === 'object' && schema.properties) {\n const properties: Record<string, unknown> = schema.properties as Record<string, unknown>;\n if (this._originalSchema) {\n const betterR: Record<string, Type> = {}\n for (const p in properties) {\n const isRequired: boolean =\n 'required' in schema && Array.isArray(schema.required) && schema.required.includes(p);\n const ch = new OpenAPIArkHelper({ value: properties[p] }, isRequired);\n if (ch.isFile()) {\n const propOriginalSchema = (this._originalSchema as Type<Record<string, unknown>>)?.props?.find(v => v.key === p)\n if (propOriginalSchema) {\n let key = p;\n if (!isRequired) {\n key = `${p}?` // set it as optional\n }\n betterR[key] = propOriginalSchema.value\n }\n }\n }\n if (Object.keys(betterR).length) {\n return type<unknown, Type<typeof betterR>>(betterR) as unknown as Record<string, unknown>\n }\n } else {\n for (const p in properties) {\n const isRequired: boolean =\n 'required' in schema && Array.isArray(schema.required) && schema.required.includes(p);\n const ch = new OpenAPIArkHelper({ value: properties[p] }, isRequired);\n if (ch.isFile()) {\n r[p] = properties[p];\n }\n }\n }\n }\n return r;\n }\n}\n\nexport class PostmanArkHelper extends PostmanJsonHelper {\n constructor(\n params: {\n value?: Type | object | unknown;\n isRoot?: boolean;\n },\n isRequired?: boolean\n ) {\n super({ ...params, value: transformValue(params.value) }, isRequired);\n }\n isValid(): boolean {\n return !!(\n this._schema &&\n typeof this._schema === 'object' &&\n !('~standard' in this._schema) &&\n (('type' in this._schema && typeof this._schema.type === 'string') ||\n ('oneOf' in this._schema && Array.isArray(this._schema.oneOf)) ||\n ('anyOf' in this._schema && Array.isArray(this._schema.anyOf)) ||\n ('enum' in this._schema && Array.isArray(this._schema.enum)))\n );\n }\n getFirstItem(): PostmanArkHelper | undefined {\n const schema = this._schema;\n\n if ('items' in schema && typeof schema.items === 'object') {\n return new PostmanArkHelper({ value: schema.items });\n }\n\n return;\n }\n getChildren(): Record<string, PostmanArkHelper> {\n const r: Record<string, PostmanArkHelper> = {};\n const schema = this._schema;\n if ('properties' in schema && typeof schema.properties === 'object' && schema.properties) {\n const properties: Record<string, unknown> = schema.properties as Record<string, unknown>;\n for (const p in properties) {\n const isRequired: boolean =\n 'required' in schema && Array.isArray(schema.required) && schema.required.includes(p);\n r[p] = new PostmanArkHelper({ value: properties[p] }, isRequired);\n }\n }\n return r;\n }\n getAlternatives(): PostmanArkHelper[] {\n const r: PostmanArkHelper[] = [];\n const schema = this._schema;\n if ('oneOf' in schema && Array.isArray(schema.oneOf)) {\n for (const p of schema.oneOf) {\n r.push(new PostmanArkHelper({ value: p }));\n }\n }\n return r;\n }\n}\n"],"mappings":";AACA,SAAS,mBAAmB,yBAAyB;AACrD,SAAS,MAAM,YAA6B;AAE5C,SAAS,eAAe,OAAiC;AACrD,MAAI,IAAa;AACjB,MAAI,SAAS,OAAO,UAAU,cAAc,kBAAkB,SAAS,OAAO,MAAM,iBAAiB,YAAY;AAC7G,QAAK,MAAe,aAAa;AAAA,MAC7B,UAAU,CAAC,MAAM;AACb,YAAIA,KAA2C,CAAC;AAChD,YAAI,cAAc;AAClB,YAAI,KAAK,WAAW,KAAK,EAAE,SAAS,OAAO,EAAE,UAAU,cAAc,UAAU,EA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