GithubHelp home page GithubHelp logo

Comments (32)

SokratisVidros avatar SokratisVidros commented on May 18, 2024 9

Same issue as the above. Could you suggest the best solution for v10?

from validator.

deankarn avatar deankarn commented on May 18, 2024 7

ok @codepushr release 9.2.0 should fix this for you.

Please let me know if it works for you 😃

NOTE: the reason I removed exists is that required implies exists and had a number of people mention that it was confusing and so now required handles both, with one tag.

from validator.

ricardojonathanromero avatar ricardojonathanromero commented on May 18, 2024 4

model

 type Toggle struct {
	Status *bool `json:"status" validate:"required"`
}

func for validate

func validateRequest(c echo.Context, payload interface{}) error {
	c.Echo().Validator = &models.Validator{Validator: validator.New()}

	if err := c.Bind(payload); err != nil {
		s.Log.Errorf("Invalid request. c.Bind() => %s", err.Error())
		return err
	}

	if err := c.Validate(payload); err != nil {
		s.Log.Errorf("Invalid request. c.Validate() => %s, \terror: %s", StructToString(payload), err.Error())
		return err
	}

	return nil
}

and it works. To validate if bool exists, you need to make bool as a pointer.

Note: I tested it in v9

from validator.

deankarn avatar deankarn commented on May 18, 2024 3

I was thinking you could make the bool value a pointer, then it would fail when no value was present, but it would still fail ifvaluewas false so that won't work.

I may look into adding a new tag, "exists" it will not have any validation functions associated with it, however will be the failure tag when value is a pointer and is value of nil.

In the mean time however, there really isn't a way to check if a bool is set.

from validator.

deankarn avatar deankarn commented on May 18, 2024 3

Hey @codepushr I dropped exists in favour of just using required and added some nullable checks so required should work just like exists did in this scenario.

if not then it could be a bug, could you provide some sample code and I'll check it out ASAP

from validator.

codepushr avatar codepushr commented on May 18, 2024 3

Sure! This should demonstrate what I mean. Basically the validation fails although I'm using a pointer to a bool with false as the value. What I'm expecting is that if I use a pointer the validation only checks if it's not nil - but in this case it also checks the underlying value - which MAY work as expected but is different from exists in v8.

package main

import (
    "log"

    v "gopkg.in/go-playground/validator.v9"
)

type Foo struct {
    Bar *bool `validate:"required"`
}

func main() {
    b := false

    model := Foo{
        Bar: &b,
    }

    err := v.New().Struct(model)

    log.Println(err)
}

from validator.

deankarn avatar deankarn commented on May 18, 2024 2

I think there may be some small confusion about how this works.

Validator validates the data within the struct after it has been in unmarshalled from JSON.

So the steps that happen are:

  • JSON post received
  • during Bind the struct is initialized
  • the JSON is unmarshalled into the structure variables

Since go is statically typed, regardless if truthiness is passed or not, the structure will always have a value and since go's bool's are not tri-state.

It does not validate the JSON but data after unmarshalling.

Sorry for no samples, answering from my phone.
Doe's this answer your question?

from validator.

deankarn avatar deankarn commented on May 18, 2024 2

Hey @davisford, guess I was closer than I though lol

Example

if you use "exists" tag like below and bool is a pointer you will get the functionality you are looking for.
NOTE: this works with any Pointer, Interface or Invalid Type.

type Thing struct {
  Truthiness *bool `json:"truthiness" validate:"exists"`
}

Notes

This change was implemented in v6 and back-ported to v5.

I Highly recommend everyone to update to v6 if possible as some things added in v6 already cannot be back-ported to v5 due to the code change between v5 and v6

UPDATE: Please let me know if this works as expected for you and I can close this issue

from validator.

encryptblockr avatar encryptblockr commented on May 18, 2024 2

@harezmii

Status *bool , If the bool value is set to a pointer, the error is fixed. Version 10.10.0

getting this error

cannot use data.IsWrite (type *bool) as type bool in field value

from validator.

Faridalim avatar Faridalim commented on May 18, 2024 2

@EduFrazao try remove required, set default value to false

from validator.

deankarn avatar deankarn commented on May 18, 2024 2

@nsmosi this has been explained many times in a number of issues. I believe it is a misunderstanding of how Go works.

as defined on your struct is a bool which has two states true and false. In go there is no such thing as an uninitialized variable and so if the value is not defined in the JSON it will be false by default on your struct.

if false being the default state if not in the JSON is desired then theres really no need to validate it at all.

if you need to detect presence then you need a third state. Changing to a pointer *bool is one way to give this third state to ensure it was set.

from validator.

deankarn avatar deankarn commented on May 18, 2024 1

I'm actually in the middle of adding the "exists" tag it's seem to work well when using bool as a pointer, will let you know when it's released; hopefully tonight.

from validator.

wstiehler avatar wstiehler commented on May 18, 2024 1

Good morning guys. Same problem as in v10 with Bool equal to false. Even passing in the request body, it returns that it is necessary to pass. Any suggestion?

image
image

from validator.

harezmii avatar harezmii commented on May 18, 2024 1

Status *bool , If the bool value is set to a pointer, the error is fixed. Version 10.10.0

from validator.

deankarn avatar deankarn commented on May 18, 2024

Give me a few minutes, I have an idea that may work for you.

Just tied up at the moment

from validator.

davisford avatar davisford commented on May 18, 2024

I was going to maybe take a stab at writing a custom validator that could just check if something exists, but I figured I'd ask here first in case I misread the situation.

from validator.

davisford avatar davisford commented on May 18, 2024

Awesome! thanks!

from validator.

davisford avatar davisford commented on May 18, 2024

Awesome -- thank you. I may have to make my own fork of gin and update to v6. Might take a bit -- sounds like you tested it out...feel free to close the issue. I appreciate the super immediate response time.

from validator.

deankarn avatar deankarn commented on May 18, 2024

no problems, yes I tested with your exact scenario with bool as a pointer and using exists flag.

P.S. @davisford I think gin allows for custom/other validation libraries to be plugged in, in this case just the updated version, however I don't have a link to the commit, or documentation.

P.P.S star if you like, just trying to get more stars than the other validators out there, even ones that are unmaintained have more stars 😦

from validator.

deankarn avatar deankarn commented on May 18, 2024

Hey @davisford I came across how to add your own validator in gin to avoid forking, it's in this issue 2nd or 3rd comment gin-gonic/gin#224 (comment)

from validator.

codepushr avatar codepushr commented on May 18, 2024

Is this still supported in v9 ? I need the same behaviour for HTTP PATCH where I have to distinguish between ommited and set values - therefore the bool pointer. I'm getting the error that exists does not exist as a validator.

EDIT: I found the exists validator in v8 - works like a charm there! No idea where it went in v9 lol.

from validator.

deankarn avatar deankarn commented on May 18, 2024

Hey @codepushr thanks for the code,

I already replicated the issue, just running last minute tests and fix will be out in a few minutes so required will now work as expected.

will tag you and this issue when making the final release.

P.S. thanks for reporting.

from validator.

codepushr avatar codepushr commented on May 18, 2024

@joeybloggs Looking good! Thanks for the amazingly quick response! :)

from validator.

FrankHitman avatar FrankHitman commented on May 18, 2024

thanks all

from validator.

lucassimon avatar lucassimon commented on May 18, 2024
package todos

import (
	"time"

	"github.com/go-playground/validator/v10"
)

// Todo This is a model for TODO
type Todo struct {
	ID        string    `json:"id"`
	Name      string    `json:"name" validate:"required"`
	Completed bool      `json:"completed" validate:"required"`
	CreatedAt time.Time `json:"created_at"`
}

// use a single instance of Validate, it caches struct info
var validate *validator.Validate

// Validate validate input data
func (t *Todo) Validate() error {
	validate := validator.New()
	return validate.Struct(t)
}

Payload:

{
	"name": "Teste",
	"completed": false
}

Response message:

{
  "error": "Key: 'Todo.Completed' Error:Field validation for 'Completed' failed on the 'required' tag"
}

Source: https://github.com/lucassimon/fiber-estudos

from validator.

thalesfsp avatar thalesfsp commented on May 18, 2024

Reopened in #714.

from validator.

KanzakiRanko1 avatar KanzakiRanko1 commented on May 18, 2024

Good morning guys. Same problem as in v10 with Bool equal to false. Even passing in the request body, it returns that it is necessary to pass. Any suggestion?

image image

Running v10.9.0 and having the same issue.

from validator.

Faridalim avatar Faridalim commented on May 18, 2024

*bool still doesn't work for me. Any ideas to solve this guys?

from validator.

EduFrazao avatar EduFrazao commented on May 18, 2024

Same problem here guys. Version v10.11.1:

type BaseMaterialData struct {
	Name        string `json:"name" validate:"required,max=125"`
	Description string `json:"description" validate:"max=256"`
	Active      bool   `json:"active" validate:"required"`
}

When a json with "active: false" arrives, validator fails.

from validator.

EduFrazao avatar EduFrazao commented on May 18, 2024

@EduFrazao try remove required, set default value to false

Thanks. I've done that as a workaround and its running.

from validator.

brnocorreia avatar brnocorreia commented on May 18, 2024

@EduFrazao try remove required, set default value to false

Hi, man! Can u show me how u did that? I'm experiencing the same error but cant change my type to *bool

from validator.

nsmosi avatar nsmosi commented on May 18, 2024

if you use

Active      bool   `json:"active" validate:"required"`

and if the payload send json {"active": false} then it will not pass the validation , but if payload send as json {"active": true} then it will pass the validation, which in both case should pass. I tried it like so:

Active      bool   `json:"active" validate:"required,boolean"` 

the same

I tried it like so:

Active      bool   `json:"active" validate:"boolean"`

it works, but when payload does not contain the active then no error occurs which is still not correct

This is my validator version:
"github.com/go-playground/validator/v10"

from validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.