镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Validation of zero values #19

Open

Description

@mex

This library currently marks a zero value for a required property as an error (

if reflect.DeepEqual(reflect.Zero(val.Type()).Interface(), val.Interface()) {
).

But this doesn't really make sense in an API, because you should be allowed to send in a 0 for an integer property or a false for a boolean property even if it is required.

In regards to integers, the minimum option should define whether 0 is allowed, not the required option.

In regards to booleans, the required check doesn't make any sense at all in its current form as you aren't able to send anything other than true.

Activity

  1. casualjim commented on Dec 20, 2016

    @casualjim
    Member

    required values are rendered as pointers normally, the zero value for that is nil
    Do you have a specification or an example where it doesn't render it as a pointer?

  2. mex commented on Dec 20, 2016

    @mex
    Author

    I have a case, where I have set a boolean as required and x-nullable: false, because I don't want the pointer. I want a value that's either true or false - and nothing else.

    "isPrimary": {
        "description": "Bla bla",
        "type": "boolean",
        "required": true,
        "x-nullable": false
    }
    

    In this case I get a validation error, if I send in "isPrimary": false, because of that validation check.

  3. casualjim commented on Dec 20, 2016

    @casualjim
    Member

    You're explicitly disabling the ability to detect if a value is required or not with x-nullable: false.
    I assume you want to fail when the key isPrimary isn't in the json? Isn't the absence of the key the same as false?

  4. mex commented on Dec 20, 2016

    @mex
    Author

    I want it to provide a validation error, if isPrimary is absent. And no, I wouldn't say so. I don't want truthy/falsy scenarios.

    I'm starting to grasp the complexity of my issue as it would require some intermediate unmarshalling into a struct with pure pointers, perform the validation and then make a conversion to my desired result of a struct with values.

  5. casualjim commented on Dec 20, 2016

    @casualjim
    Member

    there are a number of helpers in the swag package to make working with pointers easier.
    https://github.com/go-openapi/swag/blob/master/convert_types.go#L66-L78

  6. mex commented on Dec 20, 2016

    @mex
    Author

    Yeah, I know about that, but I just don't want those conversions everywhere in my code (we are talking about around 100 endpoints with 10+ properties a piece).

    It makes for ugly code to use pointers for required and values for optional in the API layer, while using values for required and pointers for optional in the database layer.

  7. casualjim commented on Dec 20, 2016

    @casualjim
    Member

    I have a PR that deals with this, but I can't merge because it's a breaking change, and a big one.
    That PR allows for detection of absence of key as well as null value and uses only value properties.
    I think the opportunity to fix this behavior will need to wait until openapi spec v3 is released so that people expect actual breakage

  8. mex commented on Dec 20, 2016

    @mex
    Author

    Cool, looking forward to that then! I'll manage with absence ~= false until then.

  9. dtravin commented on Oct 14, 2017

    @dtravin

    @casualjim can you publish the PR you mentioned?
    I guess there is a place for RequiredBool method that does not have the reflect.DeepEqual check

  10. fredbi commented on Dec 23, 2017

    @fredbi
    Member

    See alse go-swagger/go-swagger#959 (related but not duplicate)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions