Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
pull/429/head
Leonardo Di Donato 6 years ago
parent 6079fcc186
commit c5423e4116
No known key found for this signature in database
GPG Key ID: BFFB24490FD2E91F
  1. 20
      baked_in.go
  2. 5
      doc.go
  3. 75
      validator_test.go

@ -5,6 +5,7 @@ import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
urn "github.com/leodido/go-urn"
"net" "net"
"net/url" "net/url"
"os" "os"
@ -98,6 +99,7 @@ var (
"email": isEmail, "email": isEmail,
"url": isURL, "url": isURL,
"uri": isURI, "uri": isURI,
"urn": isURN, // RFC 2141
"file": isFile, "file": isFile,
"base64": isBase64, "base64": isBase64,
"base64url": isBase64URL, "base64url": isBase64URL,
@ -1077,6 +1079,24 @@ func isURL(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %T", field.Interface())) panic(fmt.Sprintf("Bad field type %T", field.Interface()))
} }
// isUrn is the validation function for validating if the current field's value is a valid URN as per RFC 2141.
func isURN(fl FieldLevel) bool {
field := fl.Field()
switch field.Kind() {
case reflect.String:
str := field.String()
_, match := urn.Parse([]byte(str))
return match
}
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
// IsFile is the validation function for validating if the current field's value is a valid file path. // IsFile is the validation function for validating if the current field's value is a valid file path.
func isFile(fl FieldLevel) bool { func isFile(fl FieldLevel) bool {
field := fl.Field() field := fl.Field()

@ -610,6 +610,11 @@ This will accept any uri the golang request uri accepts
Usage: uri Usage: uri
URN String
This validataes that a string valud contains a valid URN
according to the RFC 2141 spec.
Base64 String Base64 String
This validates that a string value contains a valid base64 value. This validates that a string value contains a valid base64 value.

@ -5748,6 +5748,81 @@ func TestIsLte(t *testing.T) {
NotEqual(t, errs, nil) NotEqual(t, errs, nil)
} }
func TestUrn(t *testing.T) {
var tests = []struct {
param string
expected bool
}{
{"urn:a:b", true},
{"urn:a::", true},
{"urn:a:-", true},
{"URN:simple:simple", true},
{"urn:urna:simple", true},
{"urn:burnout:nss", true},
{"urn:burn:nss", true},
{"urn:urnurnurn:x", true},
{"urn:abcdefghilmnopqrstuvzabcdefghilm:x", true},
{"URN:123:x", true},
{"URN:abcd-:x", true},
{"URN:abcd-abcd:x", true},
{"urn:urnx:urn", true},
{"urn:ciao:a:b:c", true},
{"urn:aaa:x:y:", true},
{"urn:ciao:-", true},
{"urn:colon:::::nss", true},
{"urn:ciao:@!=%2C(xyz)+a,b.*@g=$_'", true},
{"URN:hexes:%25", true},
{"URN:x:abc%1Dz%2F%3az", true},
{"URN:foo:a123,456", true},
{"urn:foo:a123,456", true},
{"urn:FOO:a123,456", true},
{"urn:foo:A123,456", true},
{"urn:foo:a123%2C456", true},
{"URN:FOO:a123%2c456", true},
{"URN:FOO:ABC%FFabc123%2c456", true},
{"URN:FOO:ABC%FFabc123%2C456%9A", true},
{"urn:ietf:params:scim:schemas:core:2.0:User", true},
{"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:meta.lastModified", true},
{"URN:-xxx:x", false},
{"urn::colon:nss", false},
{"urn:abcdefghilmnopqrstuvzabcdefghilmn:specificstring", false},
{"URN:a!?:x", false},
{"URN:#,:x", false},
{"urn:urn:NSS", false},
{"urn:URN:NSS", false},
{"urn:white space:NSS", false},
{"urn:concat:no spaces", false},
{"urn:a:%", false},
{"urn:", false},
}
validate := New()
for i, test := range tests {
errs := validate.Var(test.param, "urn")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d URN failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d URN failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "urn" {
t.Fatalf("Index: %d URN failed Error: %s", i, errs)
}
}
}
}
i := 1
PanicMatches(t, func() { validate.Var(i, "urn") }, "Bad field type int")
}
func TestUrl(t *testing.T) { func TestUrl(t *testing.T) {
var tests = []struct { var tests = []struct {

Loading…
Cancel
Save