ajv-validator/ajv

ajv schema with key or id "first" already exists

hir06 opened this issue · 2 comments

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?

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.

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