-
-
Notifications
You must be signed in to change notification settings - Fork 241
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
fix #894: oas3-valid-(content-)schema-example has problem with nullable #914
Conversation
…oblem with nullable
Can someone help and explain why the change results in an additional "meta-schema not available" in the console? That's why the tests are failing, but I don't see a connection between nullable and meta schemas. Edit: Maybe spectral should use the official OpenAPI schemas as meta schemas when validating? Otherwise you'll probably run in more problems as JSON Schema draft 4 != OpenAPI Schema... |
@m-mohr spectral/src/functions/schema.ts Line 37 in ead745a
You might also need to remove a couple of addMetaSchema calls.LMK how it goes! |
Thanks, @P0lip. I tried to even use the OpenAPI Schemas to validate the schemas (see commit 31fc677) as OpenAPI Schemas != JSON Schema, but couldn't properly test it due to #919 so dropped that effort. Now I'm trying the simpler approach and to just get the JSON Schemas / ajv working with nullable, which seems to succeed. I tried to leave the schema function working also for non OpenAPI stuff by incorporating a version switch. I think this is required anyway in the long run when you really want to validate OpenAPI schemas separately from JSON Schemas as they are not the same, are different between OAS versions and I guess there will be more issues coming up for OAS schema validation in the future. Let me know what you think about all this. PS: I'm never used TypeScript before, I hope things are not too "javascripty". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one comment regarding usage of 0
.
Besides that, looking very solid.
Thanks a lot for the contribution ❤️
src/functions/schema.ts
Outdated
@@ -13,6 +12,8 @@ const betterAjvErrors = require('better-ajv-errors/lib/modern'); | |||
|
|||
export interface ISchemaOptions { | |||
schema: object; | |||
// The oasVersion, either 2 or 3 for OpenAPI Spec versions, or undefined / 0 for JSON Schema |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// The oasVersion, either 2 or 3 for OpenAPI Spec versions, or undefined / 0 for JSON Schema | |
// The oasVersion, either 2 or 3 for OpenAPI Spec versions, or null / 0 for JSON Schema |
Let's go with null instead of 0.
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose 0 so that it can be used in the ajvInstances object as key (and because I was confused with how to enforce null as default value in TS).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, just ungrouping them helps. What a silly linting rule?! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@m-mohr Wow! You're on a roll!
@@ -65,7 +81,8 @@ function getSchemaId(schemaObj: JSONSchema): void | string { | |||
} | |||
|
|||
const validators = new (class extends WeakMap<JSONSchema, ValidateFunction> { | |||
public get(schemaObj: JSONSchema) { | |||
public get(schemaObj: JSONSchema, oasVersion?: Optional<number>) { | |||
const ajv = getAjv(oasVersion); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@P0lip how cheap is it to recreate a new instance in that layer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only three instances are created, one for each "schema version" (i.e. oas2, oas3 and json schema). So overhead should be minimal and better than creating one ajv instance per example validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it should be cheap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Co-Authored-By: Jakub Rożek <jakub@rozek.tech>
Fixes #894.
Checklist
Does this PR introduce a breaking change?
Additional context
This PR tries to solve #894.
As the documentation for schemaPath states the sentence below, I guess it's fine to just turn on nullable in ajv without thinking about implications for non OpenAPI documents: