Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using ajv-errors package I get this error "Cannot read property 'allErrors' of undefined" #93

Open
lukejustaskmax opened this issue Apr 9, 2021 · 1 comment

Comments

@lukejustaskmax
Copy link

Hi I am trying to use this ajv-errors package with react and typescript but I keep getting this error even though allErrors is set true? my code is as follows:

`import Ajv, { JSONSchemaType } from 'ajv'
import addFormats from 'ajv-formats'
import ajvErrors from 'ajv-errors'

const ajv = new Ajv({ allErrors: true, $data: true })

ajvErrors(addFormats(ajv))

export const emailSchema: JSONSchemaType = {
type: 'string',
minLength: 1,
format: 'email',
errorMessage: 'test',
}

export const passwordSchema: JSONSchemaType = {
type: 'string',
minLength: 12,
maxLength: 100,
pattern: '(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.\W)',
errorMessage: 'test',
}

export const recaptchaTockenSchema: JSONSchemaType = {
type: 'string',
minLength: 1,
errorMessage: 'test',
}

const passwordValidator = ajv.compile(passwordSchema)
const emailValidator = ajv.compile(emailSchema)

export const validatePassword = (value: string) => {
return {
isValid: passwordValidator(value),
error: passwordValidator.errors ? passwordValidator.errors[0]?.message : '',
}
}

export const validateEmail = (value: string) => {
return {
isValid: emailValidator(value),
error: emailValidator.errors ? emailValidator.errors[0]?.message : '',
}
}`

@dieterc
Copy link

dieterc commented Oct 1, 2021

I had the same issue when I didn't specify the version of the package when installing via npm. After removing all the ajv packages and checking out package-lock.json I installed the packages again with the correct version specified which solved my issue. I installed the packages like this:

npm install ajv-keywords@5.0.0 ajv@8.6.3 ajv-errors@3.0.0 --save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants