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

Only perform minItems validation when field is provided #122

Merged
merged 1 commit into from
Sep 28, 2023

Conversation

nick-jones
Copy link
Contributor

As reported in #86, minItems validation is being applied even when a non-required field is omitted from the input json.

A json schema like

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "https://example.com/array",
  "type": "object",
  "properties": {
    "myArray": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "minItems": 1,
      "maxItems": 3
    },
  }
}

should pass against an empty object input

{}

(can be tested at https://www.jsonschemavalidator.net/)

Instead the generated code will result in an error during unmarshal. The proposed solution is to just add a nil check - non-required slices use omitempty, which means they end up nil if not supplied. If the field is required, the earlier field presence check will catch these - but also they'll never be nil because omitempty is not used under such circumstances. So overall this additional constraint seems safe to add. Happy to take direction on other approaches.

@codecov
Copy link

codecov bot commented Sep 28, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (0f951fd) 78.14% compared to head (5f48a22) 78.14%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #122   +/-   ##
=======================================
  Coverage   78.14%   78.14%           
=======================================
  Files          10       10           
  Lines        1446     1446           
=======================================
  Hits         1130     1130           
  Misses        244      244           
  Partials       72       72           
Files Coverage Δ
pkg/generator/validator.go 95.23% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@omissis omissis merged commit 8c546e6 into omissis:main Sep 28, 2023
3 checks passed
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.

None yet

2 participants