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

fix: improve schema validation #1071

Merged
merged 15 commits into from
Apr 12, 2020
Merged

fix: improve schema validation #1071

merged 15 commits into from
Apr 12, 2020

Conversation

P0lip
Copy link
Contributor

@P0lip P0lip commented Apr 7, 2020

Fixes #403

Checklist

  • Tests added / updated
  • Docs added / updated

Does this PR introduce a breaking change?

  • Yes
  • No

Besides addressing 403, it also improves the general readability and consistency of error messages generated by schema function.

@P0lip P0lip added the t/bug Something isn't working label Apr 7, 2020
@P0lip P0lip requested a review from nulltoken April 7, 2020 19:16
@P0lip P0lip self-assigned this Apr 7, 2020
@marbemac
Copy link
Contributor

marbemac commented Apr 7, 2020

Prove it - let's see that before and after screenshot for whatever cases were improved :P.

@P0lip
Copy link
Contributor Author

P0lip commented Apr 7, 2020

Given

openapi: 3.0.1
info:
  title: Example $ref error
  version: 1.0.0
paths:
  /user:
    get:
      operationId: getUser
      responses:
        "200":
          description: An Example
          content:
            application/json:
              schema:
                type: object
                properties:
                  user_id: 12345

5.3.0
image

PR
image

Given

openapi: 3.0.1
info:
  title: Example $ref error
  version: 1.0.0
paths:
  /user:
    get:
      operationId: getUser
      responses:
        "200":
          description: An Example
          content:
            application/json:
              schema:
                type: object
                properties:

5.3.0
image

PR
image

Given

openapi: 3.0.1
info:
  title: Example $ref error
  version: 1.0.0
paths:
  /user:
    get:
      operationId: getUser
      responses:
        "200":
          description: An Example
          parameters:

5.3.0
image

PR
image

Given

openapi: 3.0.1
info:
  title: Example $ref error
  version: 1.0.0
paths:
  /user:
    get:
      operationId: getUser
      parameters:
        - type: string
          name: module_id
          in: path
          required: true
          schema:
            type: string
          description: Module ID
      responses:

5.3.0
image

PR
image

Etc, etc.

src/functions/schema.ts Outdated Show resolved Hide resolved
import { IFunction, IFunctionContext } from '../../../types';

// /shrug
function prepareResults(errors: AJV.ErrorObject[]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@P0lip As this is about to become a somewhat cornerstone of schema validation, how about covering it with targeted unit tests to better express what we expect from this function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Also more comments on why would help future maintainers, our future selves (and my current self) understand whats going on better.

src/functions/schema.ts Show resolved Hide resolved
src/functions/schema.ts Show resolved Hide resolved
@philsturgeon
Copy link
Contributor

@P0lip this is amazing! It's the biggest issue we have with Spectral and it looks like you've nailed it.

I know it might feel annoying and redundant, but can you make a test harness for each of those examples you put in? As it's such a complicated and important thing, I want to smother it with coverage so it doesn't rear its ugly head again.

@P0lip P0lip requested review from philsturgeon and nulltoken April 8, 2020 18:54
@P0lip P0lip force-pushed the fix/schema-validation branch from 31034f0 to 8cf81f2 Compare April 8, 2020 19:23
test-harness/scenarios/oas3-schema.scenario Outdated Show resolved Hide resolved
src/functions/schema.ts Show resolved Hide resolved
src/functions/schema.ts Show resolved Hide resolved
@P0lip P0lip requested a review from nulltoken April 9, 2020 11:26
@P0lip
Copy link
Contributor Author

P0lip commented Apr 10, 2020

@nulltoken @philsturgeon do you have any additional comments or suggestions?

@nulltoken
Copy link
Contributor

@nulltoken @philsturgeon do you have any additional comments or suggestions?

To be honest, I've read the code of prepareResults at least three times, I still have no idea what it exactly does. Would it be very time consuming to export it and cover it with tests putting under the lights the input AJV-like error objects and the expected results?

By taking a look at the results of the integration tests, I can see that it indeed works, however, I'd really like to get a better understanding at how it works and solves the initial issue.

If we ever need to extend/patch this function, I'd feel more at ease with a set of tight unit tests that precisely express the behavioral contract of the function.

@P0lip
Copy link
Contributor Author

P0lip commented Apr 10, 2020

To be honest, I've read the code of prepareResults at least three times, I still have no idea what it exactly does. Would it be very time consuming to export it and cover it with tests putting under the lights the input AJV-like error objects and the expected results?

Yep, I'll prepare errors. It'll certainly make it easier for everyone to reason about that function when you show what kind of errors it filters out and how it does it.

@P0lip
Copy link
Contributor Author

P0lip commented Apr 10, 2020

@nulltoken added some. LMK if it's more clear now. In general all it does is removal of "evil" errors.

@P0lip
Copy link
Contributor Author

P0lip commented Apr 12, 2020

@nulltoken done!

@huksley
Copy link

huksley commented Apr 15, 2020

Tried to use latest develop branch with this fix applied, have a different error from my issue I have
maasglobal/maas-tsp-api#70 (comment)

/Users/user/src/maas-tsp-api/schemas/core/components/travel-mode.json
 1:1  error  oas3-schema  Property `$id` is not expected to be here.

I see no problem with this JSON schema file, see https://github.com/maasglobal/maas-schemas/blob/develop/maas-schemas/schemas/core/components/travel-mode.json

@P0lip
Copy link
Contributor Author

P0lip commented Apr 15, 2020

Is $id considered a valid property in OAS-flavor of JSON Schema?
I'm not sure and cannot seem to find anything about that in docs.
I've checked the JSON Schema model we use for validation and it has no trace of $id, therefore the report is likely to be correct.
The question is whether $id is actually allowed or not - if it is, we'll need to adjust our JSON Schema model.

@philsturgeon any clue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t/bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid schema causes "should have required property '$ref'" error
5 participants