Example #1
0
func Test_TimeIsPresent(t *testing.T) {
	a := assert.New(t)
	v := TimeIsPresent{"Created At", time.Now()}
	es := validate.NewErrors()
	v.IsValid(es)
	a.Equal(0, es.Count())

	v = TimeIsPresent{"Created At", time.Time{}}
	v.IsValid(es)
	a.Equal(1, es.Count())
	a.Equal(es.Get("created_at"), []string{"Created At can not be blank."})
}
Example #2
0
func Test_IntIsPresent(t *testing.T) {
	assert := assert.New(t)

	v := IntIsPresent{"Name", 1}
	errors := validate.NewErrors()
	v.IsValid(errors)
	assert.Equal(errors.Count(), 0)

	v = IntIsPresent{"Name", 0}
	v.IsValid(errors)
	assert.Equal(errors.Count(), 1)
	assert.Equal(errors.Get("name"), []string{"Name can not be blank."})
}
func Test_IntArrayIsPresent(t *testing.T) {
	assert := assert.New(t)

	v := IntArrayIsPresent{"Name", []int{1}}
	errors := validate.NewErrors()
	v.IsValid(errors)
	assert.Equal(errors.Count(), 0)

	v = IntArrayIsPresent{"Name", []int{}}
	v.IsValid(errors)
	assert.Equal(errors.Count(), 1)
	assert.Equal(errors.Get("name"), []string{"Name can not be empty."})
}
Example #4
0
func Test_StringInclusion(t *testing.T) {
	r := require.New(t)

	l := []string{"Mark", "Bates"}

	v := StringInclusion{"Name", "Mark", l}
	errors := validate.NewErrors()
	v.IsValid(errors)
	r.Equal(errors.Count(), 0)

	v = StringInclusion{"Name", "Foo", l}
	v.IsValid(errors)
	r.Equal(errors.Count(), 1)
	r.Equal(errors.Get("name"), []string{"Name is not in the list [Mark, Bates]."})
}
Example #5
0
func Test_FuncValidator(t *testing.T) {
	r := require.New(t)

	fv := &validators.FuncValidator{
		Field:   "Name",
		Message: "%s can't be blank",
		Fn: func() bool {
			return false
		},
	}

	verrs := validate.NewErrors()
	fv.IsValid(verrs)

	r.Equal([]string{"Name can't be blank"}, verrs.Get("name"))
}
func Test_TimeIsBeforeTime(t *testing.T) {
	a := assert.New(t)
	now := time.Now()
	v := TimeIsBeforeTime{
		FirstName: "Opens At", FirstTime: now,
		SecondName: "Closes At", SecondTime: now.Add(100000),
	}

	es := validate.NewErrors()
	v.IsValid(es)
	a.Equal(0, es.Count())

	v.SecondTime = now.Add(-100000)
	v.IsValid(es)

	a.Equal(1, es.Count())
	a.Equal(es.Get("opens_at"), []string{"Opens At must be before Closes At."})
}