From e77697719f0ae464d143126b3cee91d83e66b464 Mon Sep 17 00:00:00 2001 From: Morgbn Date: Wed, 15 Nov 2023 14:09:53 +0100 Subject: [PATCH] feat: :sparkles: provide `useOaModelAjv` to access ajv instance & addKeywords --- playground/ajv-keywords.ts | 20 ++++++++++++++++++++ playground/app.vue | 3 ++- playground/schemas/Todo.json | 4 ++++ playground/server/api/[...todos].ts | 5 +++++ src/module.ts | 2 +- src/runtime/server/helpers/model.ts | 22 +++++++++++++--------- 6 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 playground/ajv-keywords.ts diff --git a/playground/ajv-keywords.ts b/playground/ajv-keywords.ts new file mode 100644 index 0000000..04519bc --- /dev/null +++ b/playground/ajv-keywords.ts @@ -0,0 +1,20 @@ +import type { KeywordDefinition } from 'ajv' + +export const keywords: KeywordDefinition[] = [{ + keyword: 'range', // example from https://ajv.js.org/keywords.html#define-keyword-with-validate-function + validate ([min, max], data, parentSchema, _dataCxt) { + return data >= min && data <= max + }, + error: { + message: ({ schema: [min, max], parentSchema }) => { + return `Value must be between ${min} and ${max} (inclusive)` + } + }, + metaSchema: { + // schema to validate keyword value + type: 'array', + items: [{ type: 'number' }, { type: 'number' }], + minItems: 2, + additionalItems: false + } +}] diff --git a/playground/app.vue b/playground/app.vue index 8ea5557..728e205 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -33,7 +33,7 @@ - +