admin管理员组

文章数量:1323682

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
Add a ment  | 

1 Answer 1

Reset to default 10

you 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