Skip to content

Commit

Permalink
feat: ✨ provide useOaModelAjv to access ajv instance & addKeywords
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgbn committed Nov 15, 2023
1 parent 16e5f2d commit e776977
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
20 changes: 20 additions & 0 deletions playground/ajv-keywords.ts
Original file line number Diff line number Diff line change
@@ -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
}
}]
3 changes: 2 additions & 1 deletion playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</template>
</json-list>
<dialog ref="dialog">
<json-schema ref="form" v-model="editedTodo" :schema="schema" :defs-schema="defsSchema" />
<json-schema ref="form" v-model="editedTodo" :schema="schema" :defs-schema="defsSchema" :keywords="keywords" />
<menu>
<button value="cancel" @click="closeTodo">
Cancel
Expand All @@ -59,6 +59,7 @@

<script lang="ts" setup>
import { ref } from 'vue'
import { keywords } from '~/ajv-keywords'
import { useFetch, useOaSchema, useOaDefsSchema } from '#imports'
const schema = useOaSchema('Todo')
Expand Down
4 changes: 4 additions & 0 deletions playground/schemas/Todo.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"$ref": "defs#/definitions/tag"
}
},
"cost": {
"type": "integer",
"range": [0, 250]
},
"done": {
"type": "string",
"format": "date-time",
Expand Down
5 changes: 5 additions & 0 deletions playground/server/api/[...todos].ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ObjectId } from 'mongodb'
import { consola } from 'consola'
import { H3Event } from 'h3'
import { keywords } from '~/ajv-keywords'

const { addKeywords } = useOaModelAjv() // need to be called before any useOaModel()
addKeywords(keywords)

const Layer = useOaModel('Layer')
consola.log(Layer.name) // model from layer base
Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default defineNuxtModule<ModuleOptions>({
nuxt.options.nitro.imports.presets = nuxt.options.nitro.imports.presets || []
nuxt.options.nitro.imports.presets.push({
from: resolve('runtime/server/helpers/model'),
imports: ['useOaModel', 'useModel']
imports: ['useOaModel', 'useOaModelAjv']
})
nuxt.options.nitro.imports.presets.push({
from: resolve('runtime/server/helpers/controllers'),
Expand Down
22 changes: 13 additions & 9 deletions src/runtime/server/helpers/model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { randomBytes } from 'node:crypto'
import { useLogger } from '@nuxt/kit'
import Ajv from 'ajv'
import addFormats from 'ajv-formats'
import type { ValidateFunction } from 'ajv'
import type { KeywordDefinition, ValidateFunction } from 'ajv'
import type { Collection, Document, ObjectId, OptionalUnlessRequiredId, WithId } from 'mongodb'
import { Hookable, type HookCallback } from 'hookable'
import { createError, type H3Event } from 'h3'
Expand All @@ -13,7 +12,6 @@ import { decrypt, encrypt } from './cipher'

import { useRuntimeConfig } from '#imports'

const logger = useLogger('nuxt-oa')
const { cipherAlgo, cipherKey, stringifiedSchemasByName, stringifiedDefsSchemas } = useRuntimeConfig().oa
const schemasByName = JSON.parse(stringifiedSchemasByName) as Record<keyof OaModels, Schema>
const defsSchemas = JSON.parse(stringifiedDefsSchemas) as DefsSchema[]
Expand Down Expand Up @@ -365,10 +363,16 @@ export function useOaModel<T extends keyof OaModels & string> (name: T): Model<T
return modelsCache[name]
}

/**
* @deprecated use 'useOaModel' instead
*/
export function useModel<T extends keyof OaModels & string> (name: T): Model<T> {
logger.warn('"useModel" is deprecated. Use "useOaModel" instead.')
return useOaModel(name)
/** Add user-defined keywords to ajv instance used in OaModel */
const addKeywords = (keywords: KeywordDefinition[]) => {
for (const keyword of keywords) {
ajv.addKeyword(keyword)
}
}

export function useOaModelAjv () {
return {
instance: ajv,
addKeywords
}
}

0 comments on commit e776977

Please sign in to comment.