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

Set default meta-schema in options #90

Closed
trikadin opened this issue Dec 14, 2015 · 6 comments
Closed

Set default meta-schema in options #90

trikadin opened this issue Dec 14, 2015 · 6 comments
Labels

Comments

@trikadin
Copy link

As for now, default validation meta-schema is v4 or (if corresponding option is true) v5-proposal. How to change this behaviour and set my custom meta-schema as default? V4 defines shemas like {} as valid, what is unacceptable (in my case) for top-level schemas.

@epoberezkin
Copy link
Member

One approach is to create your own meta-schema (that would have minProperties: 1, I guess), add it to ajv (with addMetaSchema) and to use its id as $schema property in your schemas. See validateSchema reference. Although I don't think that limiting the standard is generally a good idea - this approach is good for extending it. Also bear in mind that if you prevent top level schema from being {} then subschemas won't be able to be empty too - schema definition is recursive.

I am not sure why do you need it though. Are your schemas supplied at run time? You can simply run additional validation on them with some simple schema if that's the case.

@trikadin
Copy link
Author

I am not sure why do you need it though. Are your schemas supplied at run time?

Yep, all of the schemas (except of core) stores externally, and it can change at runtime -- that's why I need extra logic to validate them.

One approach is to create your own meta-schema (that would have minProperties: 1, I guess), add it to ajv (with addMetaSchema) and to use its id as $schema property in your schemas. See validateSchema reference.

Schemas without $schema will fallback to the default v4/v5 schema.

@epoberezkin
Copy link
Member

Yes, they will fallback. Rightly so :)

In any case, I think the better approach is to impose any additional requirements via some additional schema (or even without schema) than to modify meta-schema. For example, you can validate all your external schemas with {minProperties: 1, additionalProperties: false}. The advantage is that the internal schemas will still be able to be empty (and it's a very useful thing in some cases...).

If you really must change the default meta-schema nobody can stop you from using the option meta: false, and then adding your own meta-schema with the same ID as the default one has, using addMetaSchema(schema, id, false) (the last false will prevent it from being validated regardless the lack of the option that prevents schema validation - there will be nothing to validate it against yet). I don't like it though as you could guess :). I think the first approach is cleaner.

@trikadin
Copy link
Author

The problem is that I have A LOT of extra-logic for my external schemas (like root-only keywords, required properties, blacklisted properties for internal schemas) :C

If you really must change the default meta-schema nobody can stop you from using the option meta: false, then adding your own meta-schema with the same ID as the default one has, using addMetaSchema(schema, id, false) (the last false will prevent it from being validated regardless the lack of the option that prevents schema validation - there will be nothing to validate it against yet). I don't like it though as you could guess :). I think the first approach is cleaner.

That's what I do right now, but it's hacky and weird, and it means I use internal API, which one can be broken without updation major version.

Is any plans to add an explicit way to set the default meta-schema?

@epoberezkin
Copy link
Member

I will make this api public, it's just changing readme... It can be useful in some cases.

I don't like the idea of making such an option though. I will think about it.

@epoberezkin
Copy link
Member

I can now see several other use cases for it. So I think it can be done.

I think that this default meta-schema should be passed in meta option as an object (but v4 meta-schema and v5 meta-schema (in case v5 option is true) still should be added) . So it can't be changed within ajv instance.

The following has to be done:

  • draft 4 meta schema should still be added if option meta is schema
  • v5 meta schema should still be added if option meta is schema and option v5 is true
  • this meta schema must have a unique ID (different from draft 4 / v5 meta-schemas id), ajv should throw an exception otherwise
  • this default meta-schema will be validated against draft 4 or v5 meta-schema?
  • update readme
  • test(s) in options.spec.js: correct meta-schemas are used for the instances with/without v5 depending on the $schema keyword absent or present

@trikadin if you still need this feature and have time to do it I would really appreciate it.

@epoberezkin epoberezkin changed the title Change default meta-schema Set default meta-schema in options Feb 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants