admin管理员组文章数量:1323505
I am trying to add custom error messages to my application input field but when I try to get this error:
Error: strict mode: unknown keyword: "errorMessage"
Here's the code.
file one:
import Ajv, { JSONSchemaType } from 'ajv'
import addFormats from 'ajv-formats'
const ajv = new Ajv({ allErrors: true, $data: true })
addFormats(ajv)
export const emailSchema: JSONSchemaType<string> = {
type: 'string',
minLength: 1,
format: 'email',
//this is whats causing the error
errorMessage: {
minLength: 'This field cannot be empty.',
format: 'Must be a valid email address.',
},
}
export const validateEmail = (value: string) => {
return {
isValid: emailValidator(value),
error: emailValidator.errors ? emailValidator.errors[0]?.message : '',
}
}
file two:
import Ajv, { JSONSchemaType } from 'ajv'
import { LoginCredentials } from './types'
import addFormats from 'ajv-formats'
import {
emailSchema,
passwordSchema,
recaptchaTockenSchema,
} from '../../utils/validation/auth-validation'
import { getFormattedErrors } from '../../utils/validation/formatter'
const ajv = new Ajv({ allErrors: true, $data: true })
addFormats(ajv)
const credentialsSchema: JSONSchemaType<LoginCredentials> = {
type: 'object',
properties: {
emailAddress: emailSchema,
password: passwordSchema,
recaptchaToken: recaptchaTockenSchema,
},
required: ['emailAddress', 'password', 'recaptchaToken'],
additionalProperties: false,
}
const credentialsValidator = ajvpile(credentialsSchema)
export const validateCredentials = (credentials: LoginCredentials) => {
return {
isValid: credentialsValidator(credentials),
errors: getFormattedErrors(credentialsValidator.errors),
}
}
I would like a different message depending on the error for instance minLength or format but I would like to define what this error message says, how can I do this?
I am trying to add custom error messages to my application input field but when I try to get this error:
Error: strict mode: unknown keyword: "errorMessage"
Here's the code.
file one:
import Ajv, { JSONSchemaType } from 'ajv'
import addFormats from 'ajv-formats'
const ajv = new Ajv({ allErrors: true, $data: true })
addFormats(ajv)
export const emailSchema: JSONSchemaType<string> = {
type: 'string',
minLength: 1,
format: 'email',
//this is whats causing the error
errorMessage: {
minLength: 'This field cannot be empty.',
format: 'Must be a valid email address.',
},
}
export const validateEmail = (value: string) => {
return {
isValid: emailValidator(value),
error: emailValidator.errors ? emailValidator.errors[0]?.message : '',
}
}
file two:
import Ajv, { JSONSchemaType } from 'ajv'
import { LoginCredentials } from './types'
import addFormats from 'ajv-formats'
import {
emailSchema,
passwordSchema,
recaptchaTockenSchema,
} from '../../utils/validation/auth-validation'
import { getFormattedErrors } from '../../utils/validation/formatter'
const ajv = new Ajv({ allErrors: true, $data: true })
addFormats(ajv)
const credentialsSchema: JSONSchemaType<LoginCredentials> = {
type: 'object',
properties: {
emailAddress: emailSchema,
password: passwordSchema,
recaptchaToken: recaptchaTockenSchema,
},
required: ['emailAddress', 'password', 'recaptchaToken'],
additionalProperties: false,
}
const credentialsValidator = ajv.pile(credentialsSchema)
export const validateCredentials = (credentials: LoginCredentials) => {
return {
isValid: credentialsValidator(credentials),
errors: getFormattedErrors(credentialsValidator.errors),
}
}
I would like a different message depending on the error for instance minLength or format but I would like to define what this error message says, how can I do this?
Share asked Apr 8, 2021 at 13:21 lukeetlukeet 5111 gold badge7 silver badges27 bronze badges 1-
You should specify option strict: false when initializing Ajv, then you can add arbitrary properties such as errorMessage to your schema:
const ajv = new Ajv({ allErrors: true, strict: false, $data: true })
– smohadjer Commented Mar 27, 2024 at 14:17
1 Answer
Reset to default 10you need to use 'ajv-errors' lib separately. Something like that:
import Ajv from 'ajv';
import addFormats from 'ajv-formats'
import ajvErrors from 'ajv-errors';
const ajv = new Ajv({ allErrors: true, $data: true });
addFormats(ajv);
ajvErrors(ajv);
本文标签: javascriptHow to create custom error messages using AJVStack Overflow
版权声明:本文标题:javascript - How to create custom error messages using AJV? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742140262a2422550.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论