ajv-validator / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
https://ajv.js.org
MIT License
13.83k stars 876 forks source link

Maybe make the first added meta default (however it is added) (README draft-04 instructions cause warnings) #1018

Open csavio opened 5 years ago

csavio commented 5 years ago

When following the instructions for validating against draft-04 multiple warnings are thrown in the console every time. Might just be a documentation issue as putting meta: false or using schemaId: 'auto' in the configuration options prevents these warnings.

What version of Ajv are you using? Does the issue happen if you use the latest version? 6.10.0 Yes, this happens on latest

Your code

const Ajv = require('ajv');
const ajv = new Ajv({schemaId: 'id'});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

Warning messages

schema $id ignored http://json-schema.org/draft-07/schema#
schema $id ignored http://json-schema.org/draft-07/schema#
schema $id ignored http://json-schema.org/draft-07/schema#

What results did you expect? No warnings when following the instructions for using draft-04

Are you going to resolve the issue? No

epoberezkin commented 5 years ago

It’s worth showing two samples: one for using ONLY draft4 (which requires setting default meta as well) and another to support draft4&7.

ericelliott commented 5 years ago

requires setting default meta as well

Where is this documented?

ericelliott commented 5 years ago

I'm trying to validate the following schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "CreateTokenTypeParams": {
      "title": "CreateTokenTypeParams",
      "type": "object",
      "properties": {
        "issuer": {
          "type": "string",
          "required": true,
          "title": "issuer",
          "arguments": []
        },
        "name": {
          "type": "string",
          "required": true,
          "title": "name",
          "arguments": []
        },
        "url": {
          "type": "string",
          "required": false,
          "title": "url",
          "arguments": []
        }
      },
      "required": [
        "issuer",
        "name"
      ]
    }
  }
}

Using this function:

const validate = (schema, data) => {
  const avg = new Ajv({
    schemaId: 'id',
    meta: false
  });
  avg.addMetaSchema(metaSchema);
  avg._opts.defaultMeta = metaSchema.id;

  const { errors } = avg.validate(schema, data);

  return errors;
};

But I get the following error:

Error: schema is invalid: data.definitions['CreateTokenTypeParams'].properties['issuer'].required should be array

This does appear to be a valid draft-04 schema according to https://www.jsonschemavalidator.net/

epoberezkin commented 5 years ago

@ericelliott "required" should be the list of properties on the object level, "required": true is invalid.

This question is not related to this issue btw.

epoberezkin commented 5 years ago

requires setting default meta as well

Where is this documented?

Good question. It was well hidden in release notes: https://github.com/epoberezkin/ajv/releases/tag/5.0.0

If you use "meta" option it will become default meta (i.e. it will be used to validate schemas without $schema).

If you use "meta: false" option and then later add schema with addMetsSchema, there will be no default meta, unless you explicitly set it.

Perhaps, it is worth to make the first added meta default (however it is added), but it should be a major version change as it may break things for some people (schemas that were not validated before witll start being validated).

ericelliott commented 5 years ago

"required" should be the list of properties on the object level, "required": true is invalid.

In the process of trying to get ajv to validate against that schema, I passed it through a filter to remove the required props from each param, and ajv ignored the required props, returned true, and produced zero errors, even when required props were missing. I gave up and wrote my own validator. I don't know if there's something else wrong with the schema, but I didn't get any hints about what else was wrong.

The schema was produced by the graphql-to-json-schema package, which I'm using so that I can use GraphQL types to generate input validations.