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

Unique tag seems doent work #1370

Open
MeNavel opened this issue Feb 12, 2025 · 1 comment
Open

Unique tag seems doent work #1370

MeNavel opened this issue Feb 12, 2025 · 1 comment
Labels

Comments

@MeNavel
Copy link

MeNavel commented Feb 12, 2025

i create request and using validator like this :

type CreateKtpRequest struct {
NIK string json:"nik" validate:"numeric,min=16,max=16,required,unique=NIK"
Nama string json:"nama" validate:"required"
TempatLahir string json:"tempat_lahir"
TanggalLahir string json:"tanggal_lahir"
JenisKelamin string json:"jenis_kelamin"
Alamat string json:"alamat"
Agama string json:"agama"
Pekerjaan string json:"pekerjaan"
}

but my API produces an error like this
"code": "99",
"message": "Validation Error",
"data": {
"NIK": "Key: 'CreateKtpRequest.NIK' Error:Field validation for 'NIK' failed on the 'unique' tag"
}

I have made sure that the data I entered is not duplicated, all tag working normal except unique tag

@nodivbyzero
Copy link
Contributor

@MeNavel

The unique validation tag is designed for arrays, slices, and maps - not for single string fields.
Since NIK is a string, the unique=NIK validation does not work as expected.


From documentation: https://pkg.go.dev/github.com/go-playground/validator/v10#hdr-Unique

For arrays & slices, unique will ensure that there are no duplicates. For maps, unique will ensure that there are no duplicate values. 
For slices of struct, unique will ensure that there are no duplicate values in a field of the struct specified via a parameter. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants