Skip to content

Commit 69c2f13

Browse files
committed
Add AJV validation libraries and integrate i18n support in jsonFormRendererApi component
1 parent a91b82d commit 69c2f13

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"@pinia/nuxt": "^0.5.1",
2525
"@quasar/extras": "^1.16.9",
2626
"@vueuse/router": "^10.7.2",
27+
"ajv": "^8.17.1",
28+
"ajv-errors": "^3.0.0",
29+
"ajv-i18n": "^4.2.0",
2730
"cookie": "^0.6.0",
2831
"fast-password-entropy": "^1.1.1",
2932
"hibp": "^14.1.2",

src/components/jsonFormRendererApi.vue

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
div
33
//- pre(v-html="JSON.stringify({ data }, null, 2)")
44
json-forms(
5+
:ajv="customAjv.value"
6+
:i18n="i18n"
57
:data="data"
68
:schema="schema"
79
:uischema="uischema"
@@ -20,8 +22,12 @@ import type { JsonFormsChangeEvent } from '@jsonforms/vue'
2022
import { QuasarJsonformRenderer } from './quasar-jsonform'
2123
import { computed, provide, ref } from 'vue'
2224
import { useFetch } from 'nuxt/app'
25+
import localize from 'ajv-i18n'
2326
import type { ErrorObject } from 'ajv'
27+
import ajvErrors from 'ajv-errors'
28+
import { createAjv } from '@jsonforms/core'
2429
import type { components, operations } from '#build/types/service-api'
30+
import def from 'ajv-i18n'
2531
type Identity = components['schemas']['IdentitiesDto'] & { _id: string }
2632
2733
const customStyle = mergeStyles(defaultStyles, {
@@ -59,6 +65,31 @@ const validations = defineModel('validations', {
5965
default: {},
6066
})
6167
68+
const createTranslator = (locale) => (key: string, defaultMessage: string | undefined, context: { error: ErrorObject }) => {
69+
const regex = /^(?!.*\s)([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)*$/
70+
if (regex.test(key) || defaultMessage) {
71+
return defaultMessage
72+
}
73+
74+
const err = [context.error]
75+
localize[locale](err)
76+
77+
return err[0].message
78+
}
79+
80+
const i18n = ref({
81+
locale: 'fr',
82+
translate: computed(() => createTranslator('fr')),
83+
})
84+
85+
const customAjv = ref(
86+
createAjv({
87+
allErrors: true,
88+
verbose: true,
89+
strict: false,
90+
}),
91+
)
92+
6293
const data = defineModel('data', {
6394
type: Object,
6495
// required: true,
@@ -88,7 +119,7 @@ const getSchemaValidations = computed(() => {
88119
instancePath: `/${key}`,
89120
keyword: 'type',
90121
params: {},
91-
})
122+
} as any)
92123
}
93124
return errorObject
94125
})

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,13 +2060,23 @@ aggregate-error@^3.0.0:
20602060
clean-stack "^2.0.0"
20612061
indent-string "^4.0.0"
20622062

2063+
ajv-errors@^3.0.0:
2064+
version "3.0.0"
2065+
resolved "https://registry.yarnpkg.com/ajv-errors/-/ajv-errors-3.0.0.tgz#e54f299f3a3d30fe144161e5f0d8d51196c527bc"
2066+
integrity sha512-V3wD15YHfHz6y0KdhYFjyy9vWtEVALT9UrxfN3zqlI6dMioHnJrqOYfyPKol3oqrnCM9uwkcdCwkJ0WUcbLMTQ==
2067+
20632068
ajv-formats@^2.1.0:
20642069
version "2.1.1"
20652070
resolved "https://registry.yarnpkg.com/ajv-formats/-/ajv-formats-2.1.1.tgz#6e669400659eb74973bbf2e33327180a0996b520"
20662071
integrity sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==
20672072
dependencies:
20682073
ajv "^8.0.0"
20692074

2075+
ajv-i18n@^4.2.0:
2076+
version "4.2.0"
2077+
resolved "https://registry.yarnpkg.com/ajv-i18n/-/ajv-i18n-4.2.0.tgz#d48750ba60e283b3dee31e3c22c8c9f7410e326f"
2078+
integrity sha512-v/ei2UkCEeuKNXh8RToiFsUclmU+G57LO1Oo22OagNMENIw+Yb8eMwvHu7Vn9fmkjJyv6XclhJ8TbuigSglPkg==
2079+
20702080
ajv@^6.12.4:
20712081
version "6.12.6"
20722082
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4"
@@ -2087,6 +2097,16 @@ ajv@^8.0.0, ajv@^8.6.1:
20872097
require-from-string "^2.0.2"
20882098
uri-js "^4.4.1"
20892099

2100+
ajv@^8.17.1:
2101+
version "8.17.1"
2102+
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.17.1.tgz#37d9a5c776af6bc92d7f4f9510eba4c0a60d11a6"
2103+
integrity sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==
2104+
dependencies:
2105+
fast-deep-equal "^3.1.3"
2106+
fast-uri "^3.0.1"
2107+
json-schema-traverse "^1.0.0"
2108+
require-from-string "^2.0.2"
2109+
20902110
ansi-colors@^4.1.3:
20912111
version "4.1.3"
20922112
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.3.tgz#37611340eb2243e70cc604cad35d63270d48781b"
@@ -3400,6 +3420,11 @@ fast-password-entropy@^1.1.1:
34003420
resolved "https://registry.yarnpkg.com/fast-password-entropy/-/fast-password-entropy-1.1.1.tgz#47ba9933095fd5a32fb184915fc8e76ee19cf429"
34013421
integrity sha512-dxm29/BPFrNgyEDygg/lf9c2xQR0vnQhG7+hZjAI39M/3um9fD4xiqG6F0ZjW6bya5m9CI0u6YryHGRtxCGCiw==
34023422

3423+
fast-uri@^3.0.1:
3424+
version "3.0.6"
3425+
resolved "https://registry.yarnpkg.com/fast-uri/-/fast-uri-3.0.6.tgz#88f130b77cfaea2378d56bf970dea21257a68748"
3426+
integrity sha512-Atfo14OibSv5wAp4VWNsFYE1AchQRTv9cBGWET4pZWHzYshFSS9NQI6I57rdKn9croWVMbYFbLhJ+yJvmZIIHw==
3427+
34033428
fastq@^1.6.0:
34043429
version "1.17.1"
34053430
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.17.1.tgz#2a523f07a4e7b1e81a42b91b8bf2254107753b47"

0 commit comments

Comments
 (0)