func TestArrayItemsValidation(t *testing.T) {
	items := spec.NewItems().CollectionOf(stringItems, "").WithMinItems(1).WithMaxItems(5).UniqueValues()
	items.WithEnum("aaa", "bbb", "ccc")
	parent := spec.QueryParam("tags").CollectionOf(items, "")
	path := parent.Name + ".1"
	validator := newItemsValidator(parent.Name, parent.In, items, parent, strfmt.Default)

	// MinItems
	err := validator.Validate(1, []string{})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, minItemsErrorItems(path, validator.in, items), err.Errors[0].Error())
	// MaxItems
	err = validator.Validate(1, []string{"a", "b", "c", "d", "e", "f"})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, maxItemsErrorItems(path, validator.in, items), err.Errors[0].Error())
	// UniqueItems
	err = validator.Validate(1, []string{"a", "a"})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, duplicatesErrorItems(path, validator.in), err.Errors[0].Error())

	// Enum
	err = validator.Validate(1, []string{"a", "b", "c"})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, enumFailItems(path, validator.in, items, []string{"a", "b", "c"}), err.Errors[0].Error())

	// Items
	strItems := spec.NewItems().WithMinLength(3).WithMaxLength(5).WithPattern(`^[a-z]+$`).Typed("string", "")
	items = spec.NewItems().CollectionOf(strItems, "").WithMinItems(1).WithMaxItems(5).UniqueValues()
	validator = newItemsValidator(parent.Name, parent.In, items, parent, strfmt.Default)

	err = validator.Validate(1, []string{"aa", "bbb", "ccc"})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, minLengthErrorItems(path+".0", parent.In, strItems), err.Errors[0].Error())
}
예제 #2
0
func TestArrayParameterValidation(t *testing.T) {
	tagsParam := spec.QueryParam("tags").CollectionOf(stringItems, "").WithMinItems(1).WithMaxItems(5).UniqueValues()
	tagsParam.WithEnum([]string{"a", "a", "a"}, []string{"b", "b", "b"}, []string{"c", "c", "c"})
	validator := NewParamValidator(tagsParam, strfmt.Default)

	// MinItems
	err := validator.Validate([]string{})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, minItemsError(tagsParam), err.Errors[0].Error())
	// MaxItems
	err = validator.Validate([]string{"a", "b", "c", "d", "e", "f"})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, maxItemsError(tagsParam), err.Errors[0].Error())
	// UniqueItems
	err = validator.Validate([]string{"a", "a"})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, duplicatesError(tagsParam), err.Errors[0].Error())

	// Enum
	err = validator.Validate([]string{"a", "b", "c"})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, enumFail(tagsParam, []string{"a", "b", "c"}), err.Errors[0].Error())

	// Items
	strItems := spec.NewItems().WithMinLength(3).WithMaxLength(5).WithPattern(`^[a-z]+$`)
	tagsParam = spec.QueryParam("tags").CollectionOf(strItems, "").WithMinItems(1).WithMaxItems(5).UniqueValues()
	validator = NewParamValidator(tagsParam, strfmt.Default)
	err = validator.Validate([]string{"aa", "bbb", "ccc"})
	assert.True(t, err.HasErrors())
	assert.EqualError(t, minLengthErrorItems("tags.0", tagsParam.In, strItems), err.Errors[0].Error())

	// Not required in a parameter or items
	// Additional items
	// AllOf
	// AnyOf
	// OneOf
	// Not
	// Definitions
}
func TestStringItemsValidation(t *testing.T) {
	items := spec.NewItems().WithMinLength(3).WithMaxLength(5).WithPattern(`^[a-z]+$`).Typed("string", "")
	items.WithEnum("aaa", "bbb", "ccc")
	parent := spec.QueryParam("tags").CollectionOf(items, "")
	path := parent.Name + ".1"
	validator := newItemsValidator(parent.Name, parent.In, items, parent, strfmt.Default)

	// required
	err := validator.Validate(1, "")
	assert.True(t, err.HasErrors())
	assert.EqualError(t, minLengthErrorItems(path, validator.in, items), err.Errors[0].Error())

	// MaxLength
	err = validator.Validate(1, "abcdef")
	assert.True(t, err.HasErrors())
	assert.EqualError(t, maxLengthErrorItems(path, validator.in, items), err.Errors[0].Error())

	// MinLength
	err = validator.Validate(1, "a")
	assert.True(t, err.HasErrors())
	assert.EqualError(t, minLengthErrorItems(path, validator.in, items), err.Errors[0].Error())

	// Pattern
	err = validator.Validate(1, "a394")
	assert.True(t, err.HasErrors())
	assert.EqualError(t, patternFailItems(path, validator.in, items), err.Errors[0].Error())

	// Enum
	err = validator.Validate(1, "abcde")
	assert.True(t, err.HasErrors())
	assert.EqualError(t, enumFailItems(path, validator.in, items, "abcde"), err.Errors[0].Error())

	// Valid passes
	err = validator.Validate(1, "bbb")
	assert.True(t, err == nil || err.IsValid())
}
func TestNumberItemsValidation(t *testing.T) {

	values := [][]interface{}{
		[]interface{}{23, 49, 56, 21, 14, 35, 28, 7, 42},
		[]interface{}{uint(23), uint(49), uint(56), uint(21), uint(14), uint(35), uint(28), uint(7), uint(42)},
		[]interface{}{float64(23), float64(49), float64(56), float64(21), float64(14), float64(35), float64(28), float64(7), float64(42)},
	}

	for i, v := range values {
		items := spec.NewItems()
		items.WithMaximum(makeFloat(v[1]), false)
		items.WithMinimum(makeFloat(v[3]), false)
		items.WithMultipleOf(makeFloat(v[7]))
		items.WithEnum(v[3], v[6], v[8], v[1])
		items.Typed("integer", "int32")
		parent := spec.QueryParam("factors").CollectionOf(items, "")
		path := fmt.Sprintf("factors.%d", i)
		validator := newItemsValidator(parent.Name, parent.In, items, parent, strfmt.Default)

		// MultipleOf
		err := validator.Validate(i, v[0])
		assert.True(t, err.HasErrors())
		assert.EqualError(t, multipleOfErrorItems(path, validator.in, items), err.Errors[0].Error())

		// Maximum
		err = validator.Validate(i, v[1])
		assert.True(t, err == nil || err.IsValid())
		err = validator.Validate(i, v[2])
		assert.True(t, err.HasErrors())
		assert.EqualError(t, maxErrorItems(path, validator.in, items), err.Errors[0].Error())

		// ExclusiveMaximum
		items.ExclusiveMaximum = true
		// requires a new items validator because this is set a creation time
		validator = newItemsValidator(parent.Name, parent.In, items, parent, strfmt.Default)
		err = validator.Validate(i, v[1])
		assert.True(t, err.HasErrors())
		assert.EqualError(t, maxErrorItems(path, validator.in, items), err.Errors[0].Error())

		// Minimum
		err = validator.Validate(i, v[3])
		assert.True(t, err == nil || err.IsValid())
		err = validator.Validate(i, v[4])
		assert.True(t, err.HasErrors())
		assert.EqualError(t, minErrorItems(path, validator.in, items), err.Errors[0].Error())

		// ExclusiveMinimum
		items.ExclusiveMinimum = true
		// requires a new items validator because this is set a creation time
		validator = newItemsValidator(parent.Name, parent.In, items, parent, strfmt.Default)
		err = validator.Validate(i, v[3])
		assert.True(t, err.HasErrors())
		assert.EqualError(t, minErrorItems(path, validator.in, items), err.Errors[0].Error())

		// Enum
		err = validator.Validate(i, v[5])
		assert.True(t, err.HasErrors())
		assert.EqualError(t, enumFailItems(path, validator.in, items, v[5]), err.Errors[0].Error())

		// Valid passes
		err = validator.Validate(i, v[6])
		assert.True(t, err == nil || err.IsValid())
	}

}
예제 #5
0
func TestValidateItems(t *testing.T) {
	doc, api := petstore.NewAPI(t)
	validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), api.Formats())
	validator.spec = doc
	res := validator.validateItems()
	assert.Empty(t, res.Errors)

	// in operation parameters
	sw := doc.Spec()
	sw.Paths.Paths["/pets"].Get.Parameters[0].Type = "array"
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	sw.Paths.Paths["/pets"].Get.Parameters[0].Items = spec.NewItems().Typed("string", "")
	res = validator.validateItems()
	assert.Empty(t, res.Errors)

	sw.Paths.Paths["/pets"].Get.Parameters[0].Items = spec.NewItems().Typed("array", "")
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	sw.Paths.Paths["/pets"].Get.Parameters[0].Items.Items = spec.NewItems().Typed("string", "")
	res = validator.validateItems()
	assert.Empty(t, res.Errors)

	// in global parameters
	sw.Parameters = make(map[string]spec.Parameter)
	sw.Parameters["other"] = *spec.SimpleArrayParam("other", "array", "csv")
	res = validator.validateItems()
	assert.Empty(t, res.Errors)

	//pp := spec.SimpleArrayParam("other", "array", "")
	//pp.Items = nil
	//sw.Parameters["other"] = *pp
	//res = validator.validateItems()
	//assert.NotEmpty(t, res.Errors)

	// in shared path object parameters
	doc, api = petstore.NewAPI(t)
	validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), api.Formats())
	validator.spec = doc
	sw = doc.Spec()

	pa := sw.Paths.Paths["/pets"]
	pa.Parameters = []spec.Parameter{*spec.SimpleArrayParam("another", "array", "csv")}
	sw.Paths.Paths["/pets"] = pa
	res = validator.validateItems()
	assert.Empty(t, res.Errors)

	pa = sw.Paths.Paths["/pets"]
	pp := spec.SimpleArrayParam("other", "array", "")
	pp.Items = nil
	pa.Parameters = []spec.Parameter{*pp}
	sw.Paths.Paths["/pets"] = pa
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	// in body param schema
	doc, api = petstore.NewAPI(t)
	validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), api.Formats())
	validator.spec = doc
	sw = doc.Spec()
	pa = sw.Paths.Paths["/pets"]
	pa.Post.Parameters[0].Schema = spec.ArrayProperty(nil)
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	// in response headers
	doc, api = petstore.NewAPI(t)
	validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), api.Formats())
	validator.spec = doc
	sw = doc.Spec()
	pa = sw.Paths.Paths["/pets"]
	rp := pa.Post.Responses.StatusCodeResponses[200]
	var hdr spec.Header
	hdr.Type = "array"
	rp.Headers = make(map[string]spec.Header)
	rp.Headers["X-YADA"] = hdr
	pa.Post.Responses.StatusCodeResponses[200] = rp
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)

	// in response schema
	doc, api = petstore.NewAPI(t)
	validator = NewSpecValidator(spec.MustLoadSwagger20Schema(), api.Formats())
	validator.spec = doc
	sw = doc.Spec()
	pa = sw.Paths.Paths["/pets"]
	rp = pa.Post.Responses.StatusCodeResponses[200]
	rp.Schema = spec.ArrayProperty(nil)
	pa.Post.Responses.StatusCodeResponses[200] = rp
	res = validator.validateItems()
	assert.NotEmpty(t, res.Errors)
}