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

ajv schema with key or id "first" already exists #2435

Closed
hir06 opened this issue May 14, 2024 · 2 comments
Closed

ajv schema with key or id "first" already exists #2435

hir06 opened this issue May 14, 2024 · 2 comments

Comments

@hir06
Copy link

hir06 commented May 14, 2024

What version of Ajv are you using? Does the issue happen if you use the latest version?
8.13.0

  • happening in latest version - yes
    Ajv options object
{}

JSON Schema

{
  "$id":"first",
  "type": "object",
  "properties": {
    "example": {
      "type": "string",
      "description": "example"
    }
  }
}

Sample data

{
  "test": "Fw"
}

Your code

   const ajv = new Ajv()
  
    const validateJsonSchema = () => {
      try {
        if (!jsonSchema || !defaultValue) {
          showError('Please provide a JSON schema and the default value to validate')
          return
        }
        const parsedSchema = JSON.parse(jsonSchema)
        // Compile the schema using AJV
        const validate = ajv.compile(parsedSchema)

        const parsedDefaultValue = JSON.parse(defaultValue)
        const isValid = validate(parsedDefaultValue)

        if (isValid) {
          showSuccess('Value provided matches the JSON schema')
        } else {
          // Display specific error messages from AJV
          const errorMessages = validate.errors
            ?.map((err) => `${err.instancePath} ${err.message}`)
            .join(', ')
          showError(`Value provided doesn't match with schema: ${errorMessages}`)
        }
        
      } catch (error: unknown) {
        showError('Error parsing JSON schema or default value. Ensure they are valid JSON.', {
          caption: error,
        })
      }
    }

Validation result, data AFTER validation, error messages

Screenshot 2024-05-14 at 12 09 40

schema with key or id already exists upon trying to validate more than once and providing id with the json-schema

What results did you expect?
to be able to validate multiple times and know the actual errors
Are you going to resolve the issue?

@jasoniangreen
Copy link
Collaborator

jasoniangreen commented May 14, 2024

Sorry @hir06 I am having trouble replicating your example. Can you recreate using this runkit template? In order to isolate the issue you are seeing it is important to be able to reduce the problem to the simplest example.

@jasoniangreen
Copy link
Collaborator

Closing as I can't repro and no further info was provided.

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

No branches or pull requests

2 participants