> Error: schema is invalid: data should be equal to constant in ajv JSON Schema

6k views Asked by At

I'm trying to compare parced JSON to JSON reference by following code:

    const Ajv = require("ajv");
    const parsedData = JSON.parse(fs.readFileSync(jsonDataPath, 'utf8'));
    const parsedSchema = JSON.parse(fs.readFileSync(jsonSchemaPath, 'utf8'));
    

    const ajvInstance = Ajv({ allErrors: true });
    const valid = ajvInstance.validate(parsedSchema, parsedData);
    if (valid) {
        console.log("User data is valid");
    } else {
        console.log("User data is INVALID!");
        console.log(ajv.errors);
    }

and got following error:

Error: schema is invalid: data should be equal to constant

As you see, I've tried converting variables to const, but it was no help.

1

There are 1 answers

7
cvekaso On

You got this error because somewhere in your JSON schema you have a definition that field is 'const'. This error is not about your JavaScript code.

The const keyword is used to restrict a value to a single value.

Read the documentation for examples and for more information const