feat(RequestBody): validation support for required fields (#6223)
fixes #5181 * application/json * application/xml * application/x-www-form-urlencoded * Set requestBodyValue values to be an immutable Map, as "value". Previously stored as a normal String. * This enables adding "errors" to the Map, for validation use * note: getOAS3RequiredRequestBodyContentType requires state.spec, * which is not available to state.oas3
This commit is contained in:
27
test/e2e-cypress/static/documents/bugs/5181.yaml
Normal file
27
test/e2e-cypress/static/documents/bugs/5181.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
info:
|
||||
title: Required parameter missing, doesn't block request from executing.
|
||||
version: '1'
|
||||
openapi: 3.0.0
|
||||
servers:
|
||||
- url: http://httpbin.org/anything
|
||||
paths:
|
||||
/foos:
|
||||
post:
|
||||
requestBody:
|
||||
content:
|
||||
application/x-www-form-urlencoded:
|
||||
# application/json:
|
||||
# application/xml:
|
||||
schema:
|
||||
properties:
|
||||
foo:
|
||||
type: string
|
||||
bar:
|
||||
type: string
|
||||
required:
|
||||
- foo
|
||||
type: object
|
||||
required: true # Note this doesn't have an impact
|
||||
responses:
|
||||
default:
|
||||
description: ok
|
||||
@@ -415,4 +415,4 @@ components:
|
||||
api_key:
|
||||
type: apiKey
|
||||
name: api_key
|
||||
in: header
|
||||
in: header
|
||||
|
||||
@@ -67,6 +67,9 @@ describe("OpenAPI 3.0 Allow Empty Values in Request Body", () => {
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// add item to pass required validation
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button")
|
||||
.click()
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
@@ -91,6 +94,9 @@ describe("OpenAPI 3.0 Allow Empty Values in Request Body", () => {
|
||||
// Request Body
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(5) > .parameters-col_description .parameter__empty_value_toggle input")
|
||||
.uncheck()
|
||||
// add item to pass required validation
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button")
|
||||
.click()
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
@@ -118,6 +124,9 @@ describe("OpenAPI 3.0 Allow Empty Values in Request Body", () => {
|
||||
.uncheck()
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(6) > .parameters-col_description .parameter__empty_value_toggle input")
|
||||
.uncheck()
|
||||
// add item to pass required validation
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button")
|
||||
.click()
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
|
||||
214
test/e2e-cypress/tests/features/oas3-request-body-required.js
Normal file
214
test/e2e-cypress/tests/features/oas3-request-body-required.js
Normal file
@@ -0,0 +1,214 @@
|
||||
/**
|
||||
* @prettier
|
||||
*/
|
||||
|
||||
describe("OpenAPI 3.0 Validation for Required Request Body and Request Body Fields", () => {
|
||||
describe("Request Body required bug/5181", () => {
|
||||
it("on execute, if empty value, SHOULD render class 'invalid' and should NOT render cURL component", () => {
|
||||
cy.visit(
|
||||
"/?url=/documents/bugs/5181.yaml"
|
||||
)
|
||||
.get("#operations-default-post_foos")
|
||||
.click()
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// get input
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(1) > .parameters-col_description input")
|
||||
.should("not.have.class", "invalid")
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
// class "invalid" should now exist (and render red, which we won't check)
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(1) > .parameters-col_description input")
|
||||
.should("have.class", "invalid")
|
||||
// cURL component should not exist
|
||||
.get(".responses-wrapper .copy-paste")
|
||||
.should("not.exist")
|
||||
})
|
||||
it("on execute, if value exists, should NOT render class 'invalid' and SHOULD render cURL component", () => {
|
||||
cy.visit(
|
||||
"/?url=/documents/bugs/5181.yaml"
|
||||
)
|
||||
.get("#operations-default-post_foos")
|
||||
.click()
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// get input
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(1) > .parameters-col_description input")
|
||||
.type("abc")
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
.should("not.have.class", "invalid")
|
||||
// cURL component should exist
|
||||
.get(".responses-wrapper .copy-paste")
|
||||
.should("exist")
|
||||
})
|
||||
})
|
||||
|
||||
describe("Request Body required fields - application/json", () => {
|
||||
it("on execute, if empty value, SHOULD render class 'invalid' and should NOT render cURL component", () => {
|
||||
cy.visit(
|
||||
"/?url=/documents/features/petstore-only-pet.openapi.yaml"
|
||||
)
|
||||
.get("#operations-pet-addPet")
|
||||
.click()
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// get and clear textarea
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .body-param textarea")
|
||||
.should("not.have.class", "invalid")
|
||||
.clear()
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
// class "invalid" should now exist (and render red, which we won't check)
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .body-param textarea")
|
||||
.should("have.class", "invalid")
|
||||
// cURL component should not exist
|
||||
.get(".responses-wrapper .copy-paste")
|
||||
.should("not.exist")
|
||||
})
|
||||
it("on execute, if value exists, even if just single space, should NOT render class 'invalid' and SHOULD render cURL component that contains the single space", () => {
|
||||
cy.visit(
|
||||
"/?url=/documents/features/petstore-only-pet.openapi.yaml"
|
||||
)
|
||||
.get("#operations-pet-addPet")
|
||||
.click()
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// get, clear, then modify textarea
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .body-param textarea")
|
||||
.clear()
|
||||
.type(" ")
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .body-param textarea")
|
||||
.should("not.have.class", "invalid")
|
||||
// cURL component should exist
|
||||
.get(".responses-wrapper .copy-paste")
|
||||
.should("exist")
|
||||
.get(".responses-wrapper .copy-paste textarea")
|
||||
.should("contains.text", "-d \" \"")
|
||||
})
|
||||
})
|
||||
|
||||
/*
|
||||
petstore ux notes:
|
||||
- required field, but if example value exists, will populate the field. So this test will clear the example value.
|
||||
- "add item" will insert an empty array, and display an input text box. This establishes a value for the field.
|
||||
*/
|
||||
describe("Request Body required fields - application/x-www-form-urlencoded", () => {
|
||||
it("on execute, if empty value, SHOULD render class 'invalid' and should NOT render cURL component", () => {
|
||||
cy.visit(
|
||||
"/?url=/documents/features/petstore-only-pet.openapi.yaml"
|
||||
)
|
||||
.get("#operations-pet-addPet")
|
||||
.click()
|
||||
.get(".opblock-section .opblock-section-request-body .body-param-content-type > select")
|
||||
.select("application/x-www-form-urlencoded")
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// get and clear input populated from example value
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(2) > .parameters-col_description input")
|
||||
.clear()
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
// class "invalid" should now exist (and render red, which we won't check)
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(2) > .parameters-col_description input")
|
||||
.should("have.class", "invalid")
|
||||
// cURL component should not exist
|
||||
.get(".responses-wrapper .copy-paste")
|
||||
.should("not.exist")
|
||||
})
|
||||
it("on execute, if all values exist, even if array exists but is empty, should NOT render class 'invalid' and SHOULD render cURL component", () => {
|
||||
cy.visit(
|
||||
"/?url=/documents/features/petstore-only-pet.openapi.yaml"
|
||||
)
|
||||
.get("#operations-pet-addPet")
|
||||
.click()
|
||||
.get(".opblock-section .opblock-section-request-body .body-param-content-type > select")
|
||||
.select("application/x-www-form-urlencoded")
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// add item to get input
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description button")
|
||||
.click()
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(2) > .parameters-col_description input")
|
||||
.should("have.value", "doggie")
|
||||
.should("not.have.class", "invalid")
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description input")
|
||||
.should("have.value", "")
|
||||
.should("not.have.class", "invalid")
|
||||
// cURL component should exist
|
||||
.get(".responses-wrapper .copy-paste")
|
||||
.should("exist")
|
||||
})
|
||||
})
|
||||
|
||||
describe("Request Body: switching between Content Types", () => {
|
||||
it("after application/json 'invalid' error, on switch content type to application/x-www-form-urlencoded, SHOULD be free of errors", () => {
|
||||
cy.visit(
|
||||
"/?url=/documents/features/petstore-only-pet.openapi.yaml"
|
||||
)
|
||||
.get("#operations-pet-addPet")
|
||||
.click()
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// get and clear textarea
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .body-param textarea")
|
||||
.should("not.have.class", "invalid")
|
||||
.clear()
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .body-param textarea")
|
||||
.should("have.class", "invalid")
|
||||
// switch content type
|
||||
.get(".opblock-section .opblock-section-request-body .body-param-content-type > select")
|
||||
.select("application/x-www-form-urlencoded")
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(2) > .parameters-col_description input")
|
||||
.should("not.have.class", "invalid")
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(4) > .parameters-col_description input")
|
||||
.should("not.have.class", "invalid")
|
||||
})
|
||||
it("after application/x-www-form-urlencoded 'invalid' error, on switch content type to application/json, SHOULD be free of errors", () => {
|
||||
cy.visit(
|
||||
"/?url=/documents/features/petstore-only-pet.openapi.yaml"
|
||||
)
|
||||
.get("#operations-pet-addPet")
|
||||
.click()
|
||||
.get(".opblock-section .opblock-section-request-body .body-param-content-type > select")
|
||||
.select("application/x-www-form-urlencoded")
|
||||
// Expand Try It Out
|
||||
.get(".try-out__btn")
|
||||
.click()
|
||||
// get and clear input
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(2) > .parameters-col_description input")
|
||||
.clear()
|
||||
// Execute
|
||||
.get(".execute.opblock-control__btn")
|
||||
.click()
|
||||
// class "invalid" should now exist (and render red, which we won't check)
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .parameters:nth-child(2) > .parameters-col_description input")
|
||||
.should("have.class", "invalid")
|
||||
// switch content type
|
||||
.get(".opblock-section .opblock-section-request-body .body-param-content-type > select")
|
||||
.select("application/json")
|
||||
.get(".opblock-body .opblock-section .opblock-section-request-body .body-param textarea")
|
||||
.should("not.have.class", "invalid")
|
||||
})
|
||||
})
|
||||
})
|
||||
508
test/mocha/core/plugins/oas3/reducers.js
Normal file
508
test/mocha/core/plugins/oas3/reducers.js
Normal file
@@ -0,0 +1,508 @@
|
||||
/* eslint-env mocha */
|
||||
import expect from "expect"
|
||||
import { fromJS } from "immutable"
|
||||
import reducer from "corePlugins/oas3/reducers"
|
||||
|
||||
describe("oas3 plugin - reducer", function () {
|
||||
describe("SET_REQUEST_BODY_VALIDATE_ERROR", () => {
|
||||
const setRequestBodyValidateError = reducer["oas3_set_request_body_validate_error"]
|
||||
|
||||
describe("missingBodyValue exists, e.g. application/json", () => {
|
||||
it("should set errors", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: "",
|
||||
requestContentType: "application/json"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = setRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
validationErrors: {
|
||||
missingBodyValue: true,
|
||||
missingRequiredKeys: []
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: "",
|
||||
requestContentType: "application/json",
|
||||
errors: ["Required field is not provided"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
|
||||
describe("missingRequiredKeys exists with length, e.g. application/x-www-form-urleconded", () => {
|
||||
it("should set nested errors", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = setRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
validationErrors: {
|
||||
missingBodyValue: null,
|
||||
missingRequiredKeys: ["name"]
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
errors: ["Required field is not provided"]
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
|
||||
it("should overwrite nested errors, for keys listed in missingRequiredKeys", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
errors: ["some fake error"]
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = setRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
validationErrors: {
|
||||
missingBodyValue: null,
|
||||
missingRequiredKeys: ["name"]
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
errors: ["Required field is not provided"]
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
|
||||
it("should not overwrite nested errors, for keys not listed in missingRequiredKeys", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
errors: ["random error should not be overwritten"]
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
errors: ["some fake error"]
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = setRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
validationErrors: {
|
||||
missingBodyValue: null,
|
||||
missingRequiredKeys: ["name"]
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
errors: ["random error should not be overwritten"]
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
errors: ["Required field is not provided"]
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
|
||||
it("should set multiple nested errors", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = setRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
validationErrors: {
|
||||
missingBodyValue: null,
|
||||
missingRequiredKeys: ["id", "name"]
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "",
|
||||
errors: ["Required field is not provided"]
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
errors: ["Required field is not provided"]
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
|
||||
describe("missingRequiredKeys is empty list", () => {
|
||||
it("should not set any errors, and return state unchanged", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = setRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
validationErrors: {
|
||||
missingBodyValue: null,
|
||||
missingRequiredKeys: []
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
|
||||
describe("other unexpected payload, e.g. no missingBodyValue or missingRequiredKeys", () => {
|
||||
it("should not throw error if receiving unexpected validationError format. return state unchanged", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = setRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
validationErrors: {
|
||||
missingBodyValue: null,
|
||||
// missingRequiredKeys: ["none provided"]
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
},
|
||||
name: {
|
||||
value: "",
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("CLEAR_REQUEST_BODY_VALIDATE_ERROR", function() {
|
||||
const clearRequestBodyValidateError = reducer["oas3_clear_request_body_validate_error"]
|
||||
|
||||
describe("bodyValue is String, e.g. application/json", () => {
|
||||
it("should clear errors", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: "{}",
|
||||
requestContentType: "application/json"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = clearRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: "{}",
|
||||
requestContentType: "application/json",
|
||||
errors: []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
|
||||
describe("bodyValue is Map with entries, e.g. application/x-www-form-urleconded", () => {
|
||||
it("should clear nested errors, and apply empty error list to all entries", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
errors: ["some random error"]
|
||||
},
|
||||
name: {
|
||||
value: "doggie",
|
||||
errors: ["Required field is not provided"]
|
||||
},
|
||||
status: {
|
||||
value: "available"
|
||||
}
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = clearRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
id: {
|
||||
value: "10",
|
||||
errors: [],
|
||||
},
|
||||
name: {
|
||||
value: "doggie",
|
||||
errors: [],
|
||||
},
|
||||
status: {
|
||||
value: "available",
|
||||
errors: [],
|
||||
},
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
|
||||
describe("bodyValue is empty Map", () => {
|
||||
it("should return state unchanged", () => {
|
||||
const state = fromJS({
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {},
|
||||
requestContentType: "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const result = clearRequestBodyValidateError(state, {
|
||||
payload: {
|
||||
path: "/pet",
|
||||
method: "post",
|
||||
}
|
||||
})
|
||||
|
||||
const expectedResult = {
|
||||
requestData: {
|
||||
"/pet": {
|
||||
post: {
|
||||
bodyValue: {
|
||||
},
|
||||
requestContentType: "application/x-www-form-urlencoded",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(result.toJS()).toEqual(expectedResult)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
Reference in New Issue
Block a user