예제 #1
0
// Used to check whether a string has at most N characters
// Fails if data is a string and its length is more than the specified comparator. Passes in all other cases.
func MaxLength(data rules.ValidationData) error {
	v, err := helper.ToString(data.Value)
	if err != nil {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is not a string",
		}
	}

	// We should always be provided with a length to validate against
	if len(data.Args) == 0 {
		return fmt.Errorf("No argument found in the validation struct (eg 'MaxLength:5')")
	}

	// Typecast our argument and test
	var max int
	if max, err = strconv.Atoi(data.Args[0]); err != nil {
		return err
	}
	// Typecast our argument and test

	if len(v) > max {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        fmt.Sprintf("is too long; it must be at most %d characters long", max),
		}
	}

	return nil
}
예제 #2
0
// Validates that a string only contains alphabetic characters
func Regexp(data rules.ValidationData) (err error) {
	v, err := helper.ToString(data.Value)
	if err != nil {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is not a string",
		}
	}

	// We should always be provided with a length to validate against
	if len(data.Args) == 0 {
		return fmt.Errorf("No argument found in the validation struct (eg 'Regexp:/^\\s+$/')")
	}

	// Remove the trailing slashes from our regex string. Regexps must be enclosed
	// within two "/" characters.
	re := data.Args[0]
	re = re[1 : len(re)-1]
	if regexp.MustCompile(re).MatchString(v) == false {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "doesn't match regular expression",
		}
	}

	return nil
}
예제 #3
0
파일: url.go 프로젝트: oivoodoo/govalidate
// Validates a URL using url.Parse() in the net/url library.
// For a valid URL, the following need to be present in a parsed URL:
// * Scheme (either http or https)
// * Host (without a backslash)
func URL(data rules.ValidationData) error {
	v, err := helper.ToString(data.Value)
	if err != nil {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is not a string",
		}
	}

	parsed, err := url.Parse(v)
	if err != nil {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is not a valid URL",
		}
	}

	if parsed.Scheme != "http" && parsed.Scheme != "https" {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        fmt.Sprintf("has an invalid scheme '%s'", parsed.Scheme),
		}
	}

	if parsed.Host == "" || strings.IndexRune(parsed.Host, '\\') > 0 {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        fmt.Sprintf("has an invalid host ('%s')", parsed.Host),
		}
	}

	return nil
}
예제 #4
0
// Checks whether a string is empty.
// Passes if the data is a non-empty string. Fails if the data isn't a string, or the data is an empty string.
func NotEmpty(data rules.ValidationData) (err error) {
	v, ok := helper.ToString(data.Value)
	if ok != nil {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is not a string",
		}
	}
	if v == "" {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is empty",
		}
	}
	return nil
}
예제 #5
0
파일: uuid.go 프로젝트: oivoodoo/govalidate
// Used to check whether a string has at most N characters
// Fails if data is a string and its length is more than the specified comparator. Passes in all other cases.
func UUID(data rules.ValidationData) error {
	v, err := helper.ToString(data.Value)
	if err != nil {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is not a string",
		}
	}

	if !IsUUID(v) {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is an invalid UUID",
		}
	}

	return nil
}
예제 #6
0
func Email(data rules.ValidationData) (err error) {
	v, ok := helper.ToString(data.Value)
	if ok != nil {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is not a string",
		}
	}

	if IsEmail(v) {
		return
	}

	return rules.ErrInvalid{
		ValidationData: data,
		Failure:        "is not a valid email address",
	}
}
예제 #7
0
// Validates that a string only contains alphabetic characters
func Alpha(data rules.ValidationData) (err error) {
	v, ok := helper.ToString(data.Value)
	if ok != nil {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "is not a string",
		}
	}

	if regexp.MustCompile(`[^a-zA-Z]+`).MatchString(v) {
		return rules.ErrInvalid{
			ValidationData: data,
			Failure:        "contains non-alphabetic characters",
		}
	}

	return nil
}