Costantin
Costantin

Reputation: 2656

no schema with key or ref with AJV when validating schema property

I'm using ajv and I want to validate only one specific property. My schema is:

{
    properties:{
        dog:{type:"string"}
    }
}

I want to know if foo is a valid dog.

I do:

const ajv = new AjvModule.default({ removeAdditional: "all", coerceTypes: true});
ajv.addSchema(require("./schemas/the_above_schema.json"))
ajv.validate(`#/dog`, 'foo');

But I get the error: Error: no schema with key or ref "#/dog"

I also tried: `ajv.addSchema(require("./schemas/the_above_schema.json"), 'dog_schema')

and ajv.validate(dog_schema#/dog`, 'foo')

What am I missing?

Upvotes: 1

Views: 6111

Answers (1)

Costantin
Costantin

Reputation: 2656

Ok I found the answer.

To validate only one specific property you can do:

ajv.addSchema(schema, schema_name)
const valid = ajv.validate(`${schema_name}#/properties/${property_name}`, value);

You can also use definitions instead of properties

Upvotes: 1

Related Questions