Update to utils.js to use a common method for asserting the validateParam function.

This commit is contained in:
Owen Conti
2017-10-11 19:19:24 -06:00
parent 7344315c34
commit 7c8942e61c

View File

@@ -274,11 +274,28 @@ describe("utils", function() {
}) })
describe("validateParam", function() { describe("validateParam", function() {
describe("OAS3 specs", function() {
let param = null let param = null
let result = null let result = null
const assertValidateParam = (param, expectedError) => {
// Swagger 2.0 version
result = validateParam( fromJS(param), false )
expect( result ).toEqual( expectedError )
// OAS3 version, using `schema` sub-object
let oas3Param = {
value: param.value || null,
required: param.required || null,
schema: {
...param,
value: undefined,
required: undefined
}
}
result = validateParam( fromJS(oas3Param), false, true )
expect( result ).toEqual( expectedError )
}
it("should check the isOAS3 flag when validating parameters", function() { it("should check the isOAS3 flag when validating parameters", function() {
// This should "skip" validation because there is no `schema.type` property // This should "skip" validation because there is no `schema.type` property
// and we are telling `validateParam` this is an OAS3 spec // and we are telling `validateParam` this is an OAS3 spec
@@ -293,127 +310,71 @@ describe("utils", function() {
expect( result ).toEqual( [] ) expect( result ).toEqual( [] )
}) })
// Test a couple examples of validateParam()
it("validates required strings", function() { it("validates required strings", function() {
// invalid string // invalid string
param = fromJS({ param = {
value: "",
required: true,
schema: {
type: "string"
}
})
result = validateParam( param, false, true )
expect( result ).toEqual( ["Required field is not provided"] )
})
it("validates required arrays", function() {
// invalid array, items do not match correct type
param = fromJS({
required: true,
value: [1],
schema: {
type: "array",
items: {
type: "string"
}
}
})
result = validateParam( param, false, true )
expect( result ).toEqual( [{index: 0, error: "Value must be a string"}] )
})
it("validates required numbers", function() {
// invalid number, string instead of a number
param = fromJS({
required: true,
value: "test",
schema: {
type: "number"
}
})
result = validateParam( param, false, true )
expect( result ).toEqual( ["Required field is not provided"] )
})
})
describe("Swagger 2.0 specs", function() {
let param = null
let result = null
it("validates required strings", function() {
// invalid string
param = fromJS({
required: true, required: true,
type: "string", type: "string",
value: "" value: ""
}) }
result = validateParam( param, false ) assertValidateParam(param, ["Required field is not provided"])
expect( result ).toEqual( ["Required field is not provided"] )
// valid string // valid string
param = fromJS({ param = {
required: true, required: true,
type: "string", type: "string",
value: "test string" value: "test string"
}) }
result = validateParam( param, false ) assertValidateParam(param, [])
expect( result ).toEqual( [] )
// valid string with min and max length // valid string with min and max length
param = fromJS({ param = {
required: true, required: true,
type: "string", type: "string",
value: "test string", value: "test string",
maxLength: 50, maxLength: 50,
minLength: 1 minLength: 1
}) }
result = validateParam( param, false ) assertValidateParam(param, [])
expect( result ).toEqual( [] )
}) })
it("validates required strings with min and max length", function() { it("validates required strings with min and max length", function() {
// invalid string with max length // invalid string with max length
param = fromJS({ param = {
required: true, required: true,
type: "string", type: "string",
value: "test string", value: "test string",
maxLength: 5 maxLength: 5
}) }
result = validateParam( param, false ) assertValidateParam(param, ["Value must be less than MaxLength"])
expect( result ).toEqual( ["Value must be less than MaxLength"] )
// invalid string with max length 0 // invalid string with max length 0
param = fromJS({ param = {
required: true, required: true,
type: "string", type: "string",
value: "test string", value: "test string",
maxLength: 0 maxLength: 0
}) }
result = validateParam( param, false ) assertValidateParam(param, ["Value must be less than MaxLength"])
expect( result ).toEqual( ["Value must be less than MaxLength"] )
// invalid string with min length // invalid string with min length
param = fromJS({ param = {
required: true, required: true,
type: "string", type: "string",
value: "test string", value: "test string",
minLength: 50 minLength: 50
}) }
result = validateParam( param, false ) assertValidateParam(param, ["Value must be greater than MinLength"])
expect( result ).toEqual( ["Value must be greater than MinLength"] )
}) })
it("validates optional strings", function() { it("validates optional strings", function() {
// valid (empty) string // valid (empty) string
param = fromJS({ param = {
required: false, required: false,
type: "string", type: "string",
value: "" value: ""
}) }
result = validateParam( param, false ) assertValidateParam(param, [])
expect( result ).toEqual( [] )
// valid string // valid string
param = fromJS({ param = fromJS({
@@ -787,7 +748,6 @@ describe("utils", function() {
expect( result ).toEqual( [] ) expect( result ).toEqual( [] )
}) })
}) })
})
describe("fromJSOrdered", () => { describe("fromJSOrdered", () => {
it("should create an OrderedMap from an object", () => { it("should create an OrderedMap from an object", () => {