Update to utils.js to use a common method for asserting the validateParam function.
This commit is contained in:
@@ -274,518 +274,478 @@ describe("utils", function() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("validateParam", function() {
|
describe("validateParam", function() {
|
||||||
|
let param = null
|
||||||
|
let result = null
|
||||||
|
|
||||||
describe("OAS3 specs", function() {
|
const assertValidateParam = (param, expectedError) => {
|
||||||
let param = null
|
// Swagger 2.0 version
|
||||||
let result = null
|
result = validateParam( fromJS(param), false )
|
||||||
|
expect( result ).toEqual( expectedError )
|
||||||
|
|
||||||
it("should check the isOAS3 flag when validating parameters", function() {
|
// OAS3 version, using `schema` sub-object
|
||||||
// This should "skip" validation because there is no `schema.type` property
|
let oas3Param = {
|
||||||
// and we are telling `validateParam` this is an OAS3 spec
|
value: param.value || null,
|
||||||
param = fromJS({
|
required: param.required || null,
|
||||||
value: "",
|
schema: {
|
||||||
required: true,
|
...param,
|
||||||
schema: {
|
value: undefined,
|
||||||
notTheTypeProperty: "string"
|
required: undefined
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
result = validateParam( param, false, true )
|
result = validateParam( fromJS(oas3Param), false, true )
|
||||||
expect( result ).toEqual( [] )
|
expect( result ).toEqual( expectedError )
|
||||||
})
|
}
|
||||||
|
|
||||||
// Test a couple examples of validateParam()
|
it("should check the isOAS3 flag when validating parameters", function() {
|
||||||
it("validates required strings", function() {
|
// This should "skip" validation because there is no `schema.type` property
|
||||||
// invalid string
|
// and we are telling `validateParam` this is an OAS3 spec
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
value: "",
|
value: "",
|
||||||
required: true,
|
required: true,
|
||||||
schema: {
|
schema: {
|
||||||
type: "string"
|
notTheTypeProperty: "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"] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false, true )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("Swagger 2.0 specs", function() {
|
it("validates required strings", function() {
|
||||||
let param = null
|
// invalid string
|
||||||
let result = null
|
param = {
|
||||||
|
required: true,
|
||||||
|
type: "string",
|
||||||
|
value: ""
|
||||||
|
}
|
||||||
|
assertValidateParam(param, ["Required field is not provided"])
|
||||||
|
|
||||||
it("validates required strings", function() {
|
// valid string
|
||||||
// invalid string
|
param = {
|
||||||
param = fromJS({
|
required: true,
|
||||||
required: true,
|
type: "string",
|
||||||
type: "string",
|
value: "test string"
|
||||||
value: ""
|
}
|
||||||
})
|
assertValidateParam(param, [])
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// valid string
|
// 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,
|
||||||
result = validateParam( param, false )
|
minLength: 1
|
||||||
expect( result ).toEqual( [] )
|
}
|
||||||
|
assertValidateParam(param, [])
|
||||||
|
})
|
||||||
|
|
||||||
// valid string with min and max length
|
it("validates required strings with min and max length", function() {
|
||||||
param = fromJS({
|
// invalid string with max length
|
||||||
required: true,
|
param = {
|
||||||
type: "string",
|
required: true,
|
||||||
value: "test string",
|
type: "string",
|
||||||
maxLength: 50,
|
value: "test string",
|
||||||
minLength: 1
|
maxLength: 5
|
||||||
})
|
}
|
||||||
result = validateParam( param, false )
|
assertValidateParam(param, ["Value must be less than MaxLength"])
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
// invalid string with max length 0
|
||||||
|
param = {
|
||||||
|
required: true,
|
||||||
|
type: "string",
|
||||||
|
value: "test string",
|
||||||
|
maxLength: 0
|
||||||
|
}
|
||||||
|
assertValidateParam(param, ["Value must be less than MaxLength"])
|
||||||
|
|
||||||
|
// invalid string with min length
|
||||||
|
param = {
|
||||||
|
required: true,
|
||||||
|
type: "string",
|
||||||
|
value: "test string",
|
||||||
|
minLength: 50
|
||||||
|
}
|
||||||
|
assertValidateParam(param, ["Value must be greater than MinLength"])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates optional strings", function() {
|
||||||
|
// valid (empty) string
|
||||||
|
param = {
|
||||||
|
required: false,
|
||||||
|
type: "string",
|
||||||
|
value: ""
|
||||||
|
}
|
||||||
|
assertValidateParam(param, [])
|
||||||
|
|
||||||
|
// valid string
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "string",
|
||||||
|
value: "test"
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
it("validates required strings with min and max length", function() {
|
it("validates required files", function() {
|
||||||
// invalid string with max length
|
// invalid file
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: true,
|
||||||
type: "string",
|
type: "file",
|
||||||
value: "test string",
|
value: undefined
|
||||||
maxLength: 5
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be less than MaxLength"] )
|
|
||||||
|
|
||||||
// invalid string with max length 0
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "string",
|
|
||||||
value: "test string",
|
|
||||||
maxLength: 0
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be less than MaxLength"] )
|
|
||||||
|
|
||||||
|
|
||||||
// invalid string with min length
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "string",
|
|
||||||
value: "test string",
|
|
||||||
minLength: 50
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be greater than MinLength"] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
it("validates optional strings", function() {
|
// valid file
|
||||||
// valid (empty) string
|
param = fromJS({
|
||||||
param = fromJS({
|
required: true,
|
||||||
required: false,
|
type: "file",
|
||||||
type: "string",
|
value: new win.File()
|
||||||
value: ""
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid string
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "string",
|
|
||||||
value: "test"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
it("validates required files", function() {
|
it("validates optional files", function() {
|
||||||
// invalid file
|
// invalid file
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: false,
|
||||||
type: "file",
|
type: "file",
|
||||||
value: undefined
|
value: "not a file"
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// valid file
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "file",
|
|
||||||
value: new win.File()
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be a file"] )
|
||||||
|
|
||||||
it("validates optional files", function() {
|
// valid (empty) file
|
||||||
// invalid file
|
param = fromJS({
|
||||||
param = fromJS({
|
required: false,
|
||||||
required: false,
|
type: "file",
|
||||||
type: "file",
|
value: undefined
|
||||||
value: "not a file"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be a file"] )
|
|
||||||
|
|
||||||
// valid (empty) file
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "file",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid file
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "file",
|
|
||||||
value: new win.File()
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
it("validates required arrays", function() {
|
// valid file
|
||||||
// invalid (empty) array
|
param = fromJS({
|
||||||
param = fromJS({
|
required: false,
|
||||||
required: true,
|
type: "file",
|
||||||
type: "array",
|
value: new win.File()
|
||||||
value: []
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// invalid (not an array)
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "array",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// invalid array, items do not match correct type
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "array",
|
|
||||||
value: [1],
|
|
||||||
items: {
|
|
||||||
type: "string"
|
|
||||||
}
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [{index: 0, error: "Value must be a string"}] )
|
|
||||||
|
|
||||||
// valid array, with no 'type' for items
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "array",
|
|
||||||
value: ["1"]
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid array, items match type
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "array",
|
|
||||||
value: ["1"],
|
|
||||||
items: {
|
|
||||||
type: "string"
|
|
||||||
}
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
it("validates optional arrays", function() {
|
it("validates required arrays", function() {
|
||||||
// valid, empty array
|
// invalid (empty) array
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: false,
|
required: true,
|
||||||
type: "array",
|
type: "array",
|
||||||
value: []
|
value: []
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// invalid, items do not match correct type
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "array",
|
|
||||||
value: ["number"],
|
|
||||||
items: {
|
|
||||||
type: "number"
|
|
||||||
}
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [{index: 0, error: "Value must be a number"}] )
|
|
||||||
|
|
||||||
// valid
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "array",
|
|
||||||
value: ["test"],
|
|
||||||
items: {
|
|
||||||
type: "string"
|
|
||||||
}
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
it("validates required booleans", function() {
|
// invalid (not an array)
|
||||||
// invalid boolean value
|
param = fromJS({
|
||||||
param = fromJS({
|
required: true,
|
||||||
required: true,
|
type: "array",
|
||||||
type: "boolean",
|
value: undefined
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// invalid boolean value (not a boolean)
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "boolean",
|
|
||||||
value: "test string"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// valid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "boolean",
|
|
||||||
value: "true"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "boolean",
|
|
||||||
value: false
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
it("validates optional booleans", function() {
|
// invalid array, items do not match correct type
|
||||||
// valid (empty) boolean value
|
param = fromJS({
|
||||||
param = fromJS({
|
required: true,
|
||||||
required: false,
|
type: "array",
|
||||||
type: "boolean",
|
value: [1],
|
||||||
value: undefined
|
items: {
|
||||||
})
|
type: "string"
|
||||||
result = validateParam( param, false )
|
}
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// invalid boolean value (not a boolean)
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "boolean",
|
|
||||||
value: "test string"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be a boolean"] )
|
|
||||||
|
|
||||||
// valid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "boolean",
|
|
||||||
value: "true"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "boolean",
|
|
||||||
value: false
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [{index: 0, error: "Value must be a string"}] )
|
||||||
|
|
||||||
it("validates required numbers", function() {
|
// valid array, with no 'type' for items
|
||||||
// invalid number, string instead of a number
|
param = fromJS({
|
||||||
param = fromJS({
|
required: true,
|
||||||
required: true,
|
type: "array",
|
||||||
type: "number",
|
value: ["1"]
|
||||||
value: "test"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// invalid number, undefined value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "number",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// valid number with min and max
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "number",
|
|
||||||
value: 10,
|
|
||||||
minimum: 5,
|
|
||||||
maximum: 99
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid negative number with min and max
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "number",
|
|
||||||
value: -10,
|
|
||||||
minimum: -50,
|
|
||||||
maximum: -5
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// invalid number with maximum:0
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "number",
|
|
||||||
value: 1,
|
|
||||||
maximum: 0
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be less than Maximum"] )
|
|
||||||
|
|
||||||
// invalid number with minimum:0
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "number",
|
|
||||||
value: -10,
|
|
||||||
minimum: 0
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be greater than Minimum"] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
it("validates optional numbers", function() {
|
// valid array, items match type
|
||||||
// invalid number, string instead of a number
|
param = fromJS({
|
||||||
param = fromJS({
|
required: true,
|
||||||
required: false,
|
type: "array",
|
||||||
type: "number",
|
value: ["1"],
|
||||||
value: "test"
|
items: {
|
||||||
})
|
type: "string"
|
||||||
result = validateParam( param, false )
|
}
|
||||||
expect( result ).toEqual( ["Value must be a number"] )
|
|
||||||
|
|
||||||
// valid (empty) number
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "number",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid number
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "number",
|
|
||||||
value: 10
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
it("validates required integers", function() {
|
it("validates optional arrays", function() {
|
||||||
// invalid integer, string instead of an integer
|
// valid, empty array
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: false,
|
||||||
type: "integer",
|
type: "array",
|
||||||
value: "test"
|
value: []
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// invalid integer, undefined value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "integer",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// valid integer
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "integer",
|
|
||||||
value: 10
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
it("validates optional integers", function() {
|
// invalid, items do not match correct type
|
||||||
// invalid integer, string instead of an integer
|
param = fromJS({
|
||||||
param = fromJS({
|
required: false,
|
||||||
required: false,
|
type: "array",
|
||||||
type: "integer",
|
value: ["number"],
|
||||||
value: "test"
|
items: {
|
||||||
})
|
type: "number"
|
||||||
result = validateParam( param, false )
|
}
|
||||||
expect( result ).toEqual( ["Value must be an integer"] )
|
|
||||||
|
|
||||||
// valid (empty) integer
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "integer",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// integers
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "integer",
|
|
||||||
value: 10
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [{index: 0, error: "Value must be a number"}] )
|
||||||
|
|
||||||
|
// valid
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "array",
|
||||||
|
value: ["test"],
|
||||||
|
items: {
|
||||||
|
type: "string"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates required booleans", function() {
|
||||||
|
// invalid boolean value
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "boolean",
|
||||||
|
value: undefined
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
|
// invalid boolean value (not a boolean)
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "boolean",
|
||||||
|
value: "test string"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
|
// valid boolean value
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "boolean",
|
||||||
|
value: "true"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// valid boolean value
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "boolean",
|
||||||
|
value: false
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates optional booleans", function() {
|
||||||
|
// valid (empty) boolean value
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "boolean",
|
||||||
|
value: undefined
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// invalid boolean value (not a boolean)
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "boolean",
|
||||||
|
value: "test string"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be a boolean"] )
|
||||||
|
|
||||||
|
// valid boolean value
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "boolean",
|
||||||
|
value: "true"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// valid boolean value
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "boolean",
|
||||||
|
value: false
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates required numbers", function() {
|
||||||
|
// invalid number, string instead of a number
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: "test"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
|
// invalid number, undefined value
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: undefined
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
|
// valid number with min and max
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: 10,
|
||||||
|
minimum: 5,
|
||||||
|
maximum: 99
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// valid negative number with min and max
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: -10,
|
||||||
|
minimum: -50,
|
||||||
|
maximum: -5
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// invalid number with maximum:0
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: 1,
|
||||||
|
maximum: 0
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be less than Maximum"] )
|
||||||
|
|
||||||
|
// invalid number with minimum:0
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: -10,
|
||||||
|
minimum: 0
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be greater than Minimum"] )
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates optional numbers", function() {
|
||||||
|
// invalid number, string instead of a number
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "number",
|
||||||
|
value: "test"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be a number"] )
|
||||||
|
|
||||||
|
// valid (empty) number
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "number",
|
||||||
|
value: undefined
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// valid number
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "number",
|
||||||
|
value: 10
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates required integers", function() {
|
||||||
|
// invalid integer, string instead of an integer
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "integer",
|
||||||
|
value: "test"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
|
// invalid integer, undefined value
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "integer",
|
||||||
|
value: undefined
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
|
// valid integer
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "integer",
|
||||||
|
value: 10
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates optional integers", function() {
|
||||||
|
// invalid integer, string instead of an integer
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "integer",
|
||||||
|
value: "test"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be an integer"] )
|
||||||
|
|
||||||
|
// valid (empty) integer
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "integer",
|
||||||
|
value: undefined
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// integers
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "integer",
|
||||||
|
value: 10
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user