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

Add minimum and maximum validations #10

Closed
wants to merge 9 commits into from

Conversation

jmaeso
Copy link

@jmaeso jmaeso commented Feb 8, 2020

No description provided.

@jmaeso
Copy link
Author

jmaeso commented Feb 8, 2020

Still needed to test some edge cases like float precisions and integer validations. Too late today >.<

@@ -73,9 +73,9 @@ type Type struct {
Ref string `json:"$ref,omitempty"` // section 7
// RFC draft-wright-json-schema-validation-00, section 5
MultipleOf int `json:"multipleOf,omitempty"` // section 5.1
Maximum int `json:"maximum,omitempty"` // section 5.2
Maximum float64 `json:"maximum,omitempty"` // section 5.2
Copy link
Author

Choose a reason for hiding this comment

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

From the specification itself... http://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.2.2 (and same for minimum)

Copy link
Author

Choose a reason for hiding this comment

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

btw, all section numbers are wrong

Copy link
Collaborator

Choose a reason for hiding this comment

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

All the section numbers refer to the quoted draft. We should sync with the latest draft.

Copy link
Author

Choose a reason for hiding this comment

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

I could do it, but probably in another PR. This one is focusing on the validations feature. Blocked by this weird bug which I don't know how to solve/avoid

pkg/schemas/model.go Outdated Show resolved Hide resolved
@jmaeso
Copy link
Author

jmaeso commented Feb 9, 2020

There are two different things to note at that point:
1- Blocked since I can't print to the file the character % for the multipleOf validation
2- Found a bug on my PR itself where multiple number validations can't be applied at once

@jmaeso jmaeso changed the title Add minimum and maximum validations wip - Add minimum and maximum validations Feb 9, 2020
@jmaeso
Copy link
Author

jmaeso commented Feb 10, 2020

Now there is just the issue with the % sign and spaces on its sides.
Don't know how to solve it.
cc/ @atombender

@jmaeso jmaeso changed the title wip - Add minimum and maximum validations Add minimum and maximum validations Feb 10, 2020
@atombender
Copy link
Collaborator

@jmaeso I'm interested in merging this if you could maybe rebase it from master?

@jmaeso
Copy link
Author

jmaeso commented Mar 1, 2021

I was testing after applying the rebase and got some issues. When reviewing the errors actually realized that master tests are not passing for me (suffix -Json is being added in all the generated structs and looks like it's not the expected behaviour). Since my code is like 1 year old now I would feel more confident if master gets fixed first in order to continue the rebase.

In case I am the only one experiencing the issue, I'm running go version go1.15.8 darwin/amd64

@jmaeso
Copy link
Author

jmaeso commented May 3, 2021

Implementation updated @atombender . Actually looks like I didn't need the % operator...
TIL % operator does not support floats: https://play.golang.org/p/H3yaNpKbJhu

ps: feel free to require a different implementation strategy

@scop
Copy link

scop commented May 7, 2021

Oh, incidentally I happened to hack on primitive validation too last night without noticing this, my approach is in #21 (only maximum/exclusiveMaximum done for now, but should be mostly trivial to add the rest). No personal preferences as to which approach to adopt as long as one is merged :) Will leave a few comments here.

@@ -642,6 +653,8 @@ func (g *schemaGenerator) generateStructType(
return nil, fmt.Errorf("could not generate type for field %q: %s", name, err)
}

g.setNumericValidations(prop, &structField)
Copy link

Choose a reason for hiding this comment

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

If I read this correctly, we add numeric validators if they're present in the schema, even if they're incorrectly set on non-numeric types, which I suppose will cause compile failures. #21 has some measures to protect against this (ignores them).

}

func (v *numericValidator) generateMultipleOf(out *codegen.Emitter) {
out.Println(`if math.Mod(%s.%s, %f) != 0 {`, varNamePlainStruct, v.fieldName, *v.multipleOf)
Copy link

Choose a reason for hiding this comment

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

IIUC non-required fields are generated as pointers and this would not handle them properly (would e.g. Mod(*float64,float64), and no nil pointer protection). But glad to be proven wrong if that's the case! Likewise for all other generate* added below. I think the approach in #21 addresses this issue.

ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"` // section 5.5
MultipleOf *float64 `json:"multipleOf,omitempty"` // section 5.1
Maximum *float64 `json:"maximum,omitempty"` // section 5.2
ExclusiveMaximum *float64 `json:"exclusiveMaximum,omitempty"` // section 5.3
Copy link

Choose a reason for hiding this comment

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

Changing Exclusive* to floats here means dropping support for draft 4 in this respect. No objections as long as it's a conscious choice. Maybe someone can figure out a clean (enough) way to support both 4 and newer? Union types would help.

@omissis omissis linked an issue Apr 8, 2023 that may be closed by this pull request
7 tasks
@omissis omissis modified the milestone: v0.10.0 Apr 8, 2023
@omissis
Copy link
Owner

omissis commented Nov 7, 2023

I am closing this PR as it's become stale, and we'll likely review the whole validation system in the future.

@omissis omissis closed this Nov 7, 2023
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.

Review and merge all upstream's pull requests
4 participants