@kaapi/validator-arktype 0.0.34 → 0.0.36
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.
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
var package_default = {
|
|
8
8
|
name: "@kaapi/validator-arktype",
|
|
9
9
|
type: "module",
|
|
10
|
-
version: "0.0.
|
|
10
|
+
version: "0.0.36",
|
|
11
11
|
private: false,
|
|
12
12
|
description: "ArkType-powered request validation and documentation plugin for Kaapi.",
|
|
13
13
|
main: "lib/index.js",
|
|
@@ -48,7 +48,7 @@ var package_default = {
|
|
|
48
48
|
"@hapi/boom": "^10.0.1",
|
|
49
49
|
"@kaapi/kaapi": "workspace:^",
|
|
50
50
|
"@novice1/api-doc-json-helper": "^1.0.8",
|
|
51
|
-
arktype: "^2.1.
|
|
51
|
+
arktype: "^2.1.29",
|
|
52
52
|
tslib: "^2.8.1"
|
|
53
53
|
},
|
|
54
54
|
devDependencies: {
|
|
@@ -189,4 +189,4 @@ export {
|
|
|
189
189
|
supportedProps,
|
|
190
190
|
validatorArk
|
|
191
191
|
};
|
|
192
|
-
//# sourceMappingURL=chunk-
|
|
192
|
+
//# sourceMappingURL=chunk-U72Z4UUB.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.36\",\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.29\",\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,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O,EAAE;AAAA,UACb,SAAS,KAAK;AAEV,kBAAM,aAAa,oBAAI,IAAY;AACnC,gBAAI;AAGJ,gBAAI,eAAe,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":[]}
|
package/lib/index.js
CHANGED
package/lib/metafile-esm.json
CHANGED
|
@@ -1 +1 @@
|
|
|
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":
|
|
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":1797,"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-U72Z4UUB.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-U72Z4UUB.js","kind":"import-statement"},{"path":"lib/chunk-QLKH743P.js","kind":"import-statement"}],"exports":["supportedProps","validatorArk"],"entryPoint":"src/validator.ts","inputs":{},"bytes":147},"lib/chunk-U72Z4UUB.js.map":{"imports":[],"exports":[],"inputs":{},"bytes":14610},"lib/chunk-U72Z4UUB.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.36",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "ArkType-powered request validation and documentation plugin for Kaapi.",
|
|
7
7
|
"main": "lib/index.js",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@hapi/boom": "^10.0.1",
|
|
30
30
|
"@novice1/api-doc-json-helper": "^1.0.8",
|
|
31
|
-
"arktype": "^2.1.
|
|
31
|
+
"arktype": "^2.1.29",
|
|
32
32
|
"tslib": "^2.8.1",
|
|
33
|
-
"@kaapi/kaapi": "^0.0.
|
|
33
|
+
"@kaapi/kaapi": "^0.0.36"
|
|
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.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,E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