Added tests for isOAS3 in validateParam function.
This commit is contained in:
@@ -137,10 +137,13 @@ export function changeParam( path, paramName, paramIn, value, isXml ){
|
||||
}
|
||||
}
|
||||
|
||||
export function validateParams( payload ){
|
||||
export const validateParams = ( payload, isOAS3 ) =>{
|
||||
return {
|
||||
type: VALIDATE_PARAMS,
|
||||
payload:{ pathMethod: payload }
|
||||
payload:{
|
||||
pathMethod: payload,
|
||||
isOAS3
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,14 +51,14 @@ export default {
|
||||
})
|
||||
},
|
||||
|
||||
[VALIDATE_PARAMS]: ( state, { payload: { pathMethod } } ) => {
|
||||
[VALIDATE_PARAMS]: ( state, { payload: { pathMethod, isOAS3 } } ) => {
|
||||
let operation = state.getIn( [ "resolved", "paths", ...pathMethod ] )
|
||||
let isXml = /xml/i.test(operation.get("consumes_value"))
|
||||
|
||||
return state.updateIn( [ "resolved", "paths", ...pathMethod, "parameters" ], fromJS([]), parameters => {
|
||||
return parameters.withMutations( parameters => {
|
||||
for ( let i = 0, len = parameters.count(); i < len; i++ ) {
|
||||
let errors = validateParam(parameters.get(i), isXml)
|
||||
let errors = validateParam(parameters.get(i), isXml, isOAS3)
|
||||
parameters.setIn([i, "errors"], fromJS(errors))
|
||||
}
|
||||
})
|
||||
|
||||
@@ -13,3 +13,7 @@ export const executeRequest = (ori, { specActions }) => (req) => {
|
||||
specActions.logRequest(req)
|
||||
return ori(req)
|
||||
}
|
||||
|
||||
export const validateParams = (ori, { specSelectors }) => (req) => {
|
||||
return ori(req, specSelectors.isOAS3())
|
||||
}
|
||||
@@ -537,12 +537,12 @@ export const validateMinLength = (val, min) => {
|
||||
}
|
||||
|
||||
// validation of parameters before execute
|
||||
export const validateParam = (param, isXml) => {
|
||||
export const validateParam = (param, isXml, isOAS3 = false) => {
|
||||
let errors = []
|
||||
let value = isXml && param.get("in") === "body" ? param.get("value_xml") : param.get("value")
|
||||
let required = param.get("required")
|
||||
|
||||
let paramDetails = param.get("schema") || param
|
||||
let paramDetails = isOAS3 ? param.get("schema") : param
|
||||
let maximum = paramDetails.get("maximum")
|
||||
let minimum = paramDetails.get("minimum")
|
||||
let type = paramDetails.get("type")
|
||||
@@ -618,7 +618,7 @@ export const validateParam = (param, isXml) => {
|
||||
|
||||
if ( !value.count() ) { return errors }
|
||||
|
||||
itemType = param.getIn(["items", "type"])
|
||||
itemType = paramDetails.getIn(["items", "type"])
|
||||
|
||||
value.forEach((item, index) => {
|
||||
let err
|
||||
|
||||
@@ -274,20 +274,73 @@ describe("utils", function() {
|
||||
})
|
||||
|
||||
describe("validateParam", function() {
|
||||
|
||||
describe("OAS3 specs", function() {
|
||||
let param = null
|
||||
let result = null
|
||||
|
||||
it("skips validation when `type` is not specified", function() {
|
||||
// invalid type
|
||||
it("should check the isOAS3 flag when validating parameters", function() {
|
||||
// This should "skip" validation because there is no `schema.type` property
|
||||
// and we are telling `validateParam` this is an OAS3 spec
|
||||
param = fromJS({
|
||||
required: false,
|
||||
type: undefined,
|
||||
value: ""
|
||||
value: "",
|
||||
required: true,
|
||||
schema: {
|
||||
notTheTypeProperty: "string"
|
||||
}
|
||||
})
|
||||
result = validateParam( param, false )
|
||||
result = validateParam( param, false, true )
|
||||
expect( result ).toEqual( [] )
|
||||
})
|
||||
|
||||
// Test a couple examples of validateParam()
|
||||
it("validates required strings", function() {
|
||||
// invalid string
|
||||
param = fromJS({
|
||||
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({
|
||||
@@ -734,6 +787,7 @@ describe("utils", function() {
|
||||
expect( result ).toEqual( [] )
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("fromJSOrdered", () => {
|
||||
it("should create an OrderedMap from an object", () => {
|
||||
|
||||
Reference in New Issue
Block a user