Code Monkey home page Code Monkey logo

validation's People

Contributors

0perl avatar aradilov avatar asdine avatar cebe avatar danail-branekov avatar davseby avatar erdaltsksn avatar jhvst avatar kfreiman avatar lyoshenka avatar maratori avatar mehran-prs avatar meydjer avatar mikeleedev avatar nathanbaulch avatar necryin avatar oliy avatar petervandenbroek avatar plbogen avatar qiangxue avatar samber avatar seriousben avatar sgleizes avatar slessard avatar swithek avatar tcmits avatar terminalfi avatar upamune avatar v4n avatar vbochko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

validation's Issues

bug about type alias

if I set a type alias of basic type,like []string ,then, I neet to add two function,such as ( value and scan ,mysql json need this), the bug is appened.
the entire code under:

package main

import (
	"database/sql/driver"
	"encoding/json"
	"fmt"

	validation "github.com/go-ozzo/ozzo-validation"
)

type Tags []string

// mysql 8.0 json need value and scan function, so I need type alias
type Courses struct {
	Tag Tags `gorm:"type:json;comment:体系名是json格式" json:"tag"`
}

// !!!!!! IF YOU REMOVE THIS FUNCTION, IT IS VALIDETED !!!!!
// !!!!!! IF YOU ADD PONITER IN P, IT IS VALIDETED  !!!!
func (p Tags) Value() (driver.Value, error) {
	b, err := json.Marshal(p)
	return b, err
}


func (p *Tags) Scan(input any) error {
	return json.Unmarshal(input.([]byte), p)
}

func main() {

	var courseDto Courses
	courseDto.Tag = Tags{"student", "engineer"}
	err := validation.ValidateStruct(&courseDto,
		validation.Field(&courseDto.Tag, validation.Required, validation.Length(1, 3)),
	)
	fmt.Println(err)
}

Something strange is coming in function value !!!
If p without pointer can't pass validate!

func (p Tags) Value() (driver.Value, error) {
	//b, err := json.Marshal(p)
	return nil, nil
}

But, if add pointer, it pass!

Please create a new release

Over the past 8 months there have been a handful of meaningful additions and changes to this project. It would be nice if these updates were available as an official release. Please consider creating a new release, v1.1.0

Add case insensitive string support for the In and NotIn rules

Currently the In and NotIn rules work just fine with strings but these two rules only work in a case sensitive way. This feature request is to add support for case insensitive In and NotIn rules.

examples:

  • In (case sensitive)
    • ("A", "B"), "A" = valid
    • ("A", "B"), "a" = invalid
  • NotIn (case insensitive)
    • ("A", "B"), "A" = invalid
    • ("A", "B"), "a" = invalid
    • ("A", "B"), "c" = valid

Conditional rule not working

This is my mode and validation receiver:

type UserSignup struct {
	ID        int        `json:"id"`
	FirstName string     `json:"first_name"`
	LastName  *string    `json:"last_name"`
	Username  *string    `json:"username"`
	Email     *string    `json:"email"`
	Phone     *string    `json:"phone"`
	Password  string    `json:"password"`
	PasswordConfirmation string `json:"password_confirmation"`
	CreatedAt *time.Time `json:"created_at"`
}

func (u *UserSignup) Validate() error {
	return validation.ValidateStruct(
		u,
		validation.Field(
			&u.FirstName,
			validation.Required,
			validation.Length(3, 80),
		),
		validation.Field(&u.LastName, validation.Length(3, 80)),
		validation.Field(&u.Username, validation.Length(3, 20)),
		validation.Field(&u.Email, is.Email, validation.When(
			&u.Phone == nil,
			validation.Required,
		)),
		validation.Field(&u.Phone, is.E164, validation.When(
			&u.Email == nil,
			validation.Required,
		)),
		validation.Field(
			&u.Password,
			validation.Required,
			validation.Length(6, 0),
		),
		validation.Field(
			&u.PasswordConfirmation,
			validation.Length(6, 0),
			validation.Required,
			validation.When(
				u.Password != u.PasswordConfirmation,
				validation.Required.Error("must be equal to `password`"),
			),
		),
	)
}

This is the validation call:

if err := validation.Validate(userSignup); err != nil {
        return err
}

These are the call args and the error:

Screenshot 2023-10-05 at 13 04 28

What is happening?

Validation is returning first_name -> the length must be between 3 and 80 only

What should happen?

Validation should return email or phone is required and also password_confirmations must be equal to password

Add nested struct validation

type Abc struct {
    A int
    B struct {
        C int
    }
}

To validate the nested struct B, a simple ValidateStruct(*Abc, Field(*Abc.B.C)) is not enough (an error is returned).

Either Field() needs to be improved or a new FieldStruct() should be created.

`In` and `NotIn` inconsistent

While using the In and NotIn rules, I've noticed that their implementations are inconsistent. The former uses DeepEqual to verify equality, whereas the latter uses the equality operator. In cases where pointers are being passed with the same underlying data, the former would work, but the latter falls short.

Can we have a discussion on whether NotIn should incorporate DeepEqual?

Possible to have "default validation" for structs?

Sorry if this is a dumb question, but I've not found a way of doing this.

I have a struct like:

type Foo struct {
    A string
    B int
    C Bar
}

I would like to ensure that Foo (and any subsequent structs like Bar) do not have any empty fields. i.e. something like:

f := Foo {} // All fields are 0-valued

if err := validation.Validate(&f, validation.Required); err != nil {
		log.Fatal(err)
	}

However, from examples it seems the only way to do this is to implement the Validatable interface for all structs you want to check. The above code excerpt will not return an error because Foo does not implement the Validatable interface.

Is there any "default" way, like the one linked above, which will be able to iterate over each field and check if the fields are non-empty? Otherwise it seems if new fields are added, the validator method(s) needs to be updated.

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.