@archtx/procedures
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
import { ErrorObject } from 'ajv'; | ||
import * as AJV from 'ajv'; | ||
import { TJSONSchema } from './types.js'; | ||
@@ -14,3 +14,3 @@ export type TSchemaParsed = { | ||
}; | ||
export type TSchemaValidationError = ErrorObject; | ||
export type TSchemaValidationError = AJV.ErrorObject; | ||
export declare function schemaParser(schema: { | ||
@@ -17,0 +17,0 @@ args?: unknown; |
@@ -1,4 +0,10 @@ | ||
import addFormats from 'ajv-formats'; | ||
import Ajv from 'ajv'; | ||
import { default as addFormats } from 'ajv-formats'; | ||
import * as AJV from 'ajv'; | ||
import { extractJsonSchema } from './extract-json-schema.js'; | ||
// @ts-ignore | ||
const ajv = addFormats(new AJV.Ajv({ | ||
allErrors: true, | ||
coerceTypes: true, | ||
removeAdditional: true, | ||
})); | ||
export function schemaParser(schema, onParseError) { | ||
@@ -27,10 +33,2 @@ const jsonSchema = {}; | ||
try { | ||
// @ts-ignore | ||
const ajv = addFormats( | ||
// @ts-ignore | ||
new Ajv({ | ||
allErrors: true, | ||
coerceTypes: true, | ||
removeAdditional: true, | ||
})); | ||
argsValidator = ajv.compile(jsonSchema.args); | ||
@@ -37,0 +35,0 @@ } |
@@ -23,2 +23,3 @@ import { describe, expect, test } from 'vitest'; | ||
await promise; | ||
await promise; | ||
}); | ||
@@ -84,3 +85,20 @@ test('it parses args and generates a validator function', async () => { | ||
}); | ||
test('it parses multiple schemas correct', async () => { | ||
const schema = schemaParser({ | ||
args: Type.Object({ a: Type.String() }), | ||
data: Type.Object({ b: Type.Null() }), | ||
}, (error) => { | ||
throw new Error(JSON.stringify(error)); | ||
}); | ||
const schema2 = schemaParser({ | ||
args: Type.Object({ c: Type.String() }), | ||
data: Type.Object({ d: Type.Number() }), | ||
}, (error) => { | ||
throw new Error(JSON.stringify(error)); | ||
}); | ||
expect(schema.validation.args?.({}).errors?.[0]?.message).toMatch(/must have required property 'a'/); | ||
expect(schema2.validation.args?.({ c: 'test' })).toMatchObject({}); | ||
expect(schema.validation.args?.({}).errors?.[0]?.message).toMatch(/must have required property 'a'/); | ||
}); | ||
}); | ||
//# sourceMappingURL=parser.test.js.map |
{ | ||
"name": "@archtx/procedures", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Procedure generator for @archtx", | ||
@@ -5,0 +5,0 @@ "main": "dist/exports.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
251212
0.74%1013
1.6%