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

Small refactoring: extract validate_enum and validate_const methods #113

Conversation

beetlegius-jt
Copy link

In order to be able to override how we're validating enums and const we need to first extract it as separate methods, so we're able to take advantage of the inheritance and override that small methods.

This won't affect the current codebase but will allow us to start working on supporting draft 2020-12 based on that.

Copy link
Owner

@davishmcclurg davishmcclurg left a comment

Choose a reason for hiding this comment

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

Can you point me to what changed for these keywords (enum and const) in the new drafts? It doesn't look like they've changed to me, but I could be missing something (looking here: https://json-schema.org/draft/2020-12/json-schema-validation.html#name-enum).

@@ -122,8 +121,8 @@ def validate_instance(instance, &block)
end
end

yield error(instance, 'enum') if enum && !enum.include?(data)
yield error(instance, 'const') if schema.key?('const') && schema['const'] != data
validate_enum(instance, &block)
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should pass enum to this method instead:

validate_enum(instance, enum, &block) if enum

yield error(instance, 'enum') if enum && !enum.include?(data)
yield error(instance, 'const') if schema.key?('const') && schema['const'] != data
validate_enum(instance, &block)
validate_const(instance, &block)
Copy link
Owner

Choose a reason for hiding this comment

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

Same here. And you could pull out const above (similar to enum on line 87):

validate_const(instance, const, &block) if schema.key?('const')

@beetlegius-jt
Copy link
Author

Can you point me to what changed for these keywords (enum and const) in the new drafts? It doesn't look like they've changed to me, but I could be missing something (looking here: https://json-schema.org/draft/2020-12/json-schema-validation.html#name-enum).

Sure, there is a new nullable boolean property, so you can mark the enum as nullable and it should skip the validation because it accepts null.

Old

language:
  oneOf:
    - type: string
      enum:
        - ruby
        - python
    - type: null

New

type: string
enum:
  - ruby
  - python
nullable: true

@davishmcclurg
Copy link
Owner

Sure, there is a new nullable boolean property, so you can mark the enum as nullable and it should skip the validation because it accepts null.

I don't see nullable in draft 2020-12 either. Looks like it might have been an OpenAPI feature that they removed?
json-schema-org/json-schema-spec#1067
OAI/OpenAPI-Specification#2246

@ahx
Copy link

ahx commented Aug 31, 2022

Looks like it might have been an OpenAPI feature that they removed?

That is correct. It was removed in OpenAPI 3.1, because you can just use multiple types.

@beetlegius-jt
Copy link
Author

Closing then people! Thanks

@beetlegius-jt beetlegius-jt deleted the extract-validate-methods branch November 8, 2022 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants