Skip to content

Commit 585a9c4

Browse files
Merge pull request wso2#480 from dulithsenanayake/context-validation-ui
Modify context validation logic
2 parents 885ba8e + f40f672 commit 585a9c4

File tree

2 files changed

+140
-23
lines changed

2 files changed

+140
-23
lines changed

portals/publisher/src/main/webapp/source/src/app/components/Apis/Create/Components/DefaultAPIForm.jsx

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ export default function DefaultAPIForm(props) {
143143
// Check the provided API validity on mount, TODO: Better to use Joi schema here ~tmkb
144144
useEffect(() => {
145145
onValidate(Boolean(api.name)
146-
&& (isAPIProduct || Boolean(api.version))
147-
&& Boolean(api.context));
146+
&& (isAPIProduct || Boolean(api.version))
147+
&& Boolean(api.context));
148148
}, []);
149149

150150
const updateValidity = (newState) => {
@@ -190,26 +190,78 @@ export default function DefaultAPIForm(props) {
190190
break;
191191
}
192192
case 'context': {
193-
const contextValidity = APIValidation.apiContext.required().validate(value, { abortEarly: false })
193+
let contextValidity = APIValidation.apiContext.required().validate(value, { abortEarly: false })
194194
.error;
195195
const apiContext = value.startsWith('/') ? value : '/' + value;
196196
if (contextValidity === null) {
197-
APIValidation.apiParameter.validate(field + ':' + apiContext).then((result) => {
198-
const count = result.body.list.length;
199-
if (count > 0 && checkContext(value, result.body.list)) {
197+
const splitContext = apiContext.split('/');
198+
for (const param of splitContext) {
199+
if (param !== null && param !== '{version}') {
200+
if (param.includes('{version}')) {
201+
contextValidity = APIValidation.apiContextWithoutKeyWords.required()
202+
.validate(value, { abortEarly: false }).error;
203+
updateValidity({
204+
...validity,
205+
// eslint-disable-next-line max-len
206+
context: { details: [{ message: '{version} cannot exist as a substring in a path param' }] },
207+
});
208+
} else if (param.includes('{') || param.includes('}')) {
209+
contextValidity = APIValidation.apiContextWithoutKeyWords.required()
210+
.validate(value, { abortEarly: false }).error;
211+
updateValidity({
212+
...validity,
213+
// eslint-disable-next-line max-len
214+
context: { details: [{ message: '{ or } cannot exist as a substring in a path param' }] },
215+
});
216+
}
217+
}
218+
}
219+
220+
let charCount = 0;
221+
222+
if (contextValidity === null) {
223+
for (const a of apiContext) {
224+
if (a === '(') {
225+
charCount++;
226+
} else if (a === ')') {
227+
charCount--;
228+
}
229+
if (charCount < 0) {
230+
updateValidity({
231+
...validity,
232+
// eslint-disable-next-line max-len
233+
context: { details: [{ message: 'Parentheses should be balanced in API context' }] },
234+
});
235+
}
236+
}
237+
238+
if (charCount > 0) {
200239
updateValidity({
201240
...validity,
202-
context: {
203-
details: [{
204-
message: isWebSocket ? apiContext + ' channel already exists'
205-
: apiContext + ' context already exists'
206-
}]
207-
},
241+
// eslint-disable-next-line max-len
242+
context: { details: [{ message: 'Parentheses should be balanced in API context' }] },
208243
});
209-
} else {
210-
updateValidity({ ...validity, context: contextValidity, version: null });
211244
}
212-
});
245+
}
246+
if (contextValidity === null && charCount === 0) {
247+
APIValidation.apiParameter.validate(field + ':' + apiContext).then((result) => {
248+
const count = result.body.list.length;
249+
if (count > 0 && checkContext(value, result.body.list)) {
250+
updateValidity({
251+
...validity,
252+
// eslint-disable-next-line max-len
253+
context: { details: [{ message: isWebSocket ? apiContext + ' channel already exists' : apiContext + ' context already exists' }] },
254+
});
255+
} else if (count > 0 && checkContext(value, result.body.list)) {
256+
updateValidity({
257+
...validity,
258+
context: { details: [{ message: apiContext + ' dynamic context already exists' }] },
259+
});
260+
} else {
261+
updateValidity({ ...validity, context: contextValidity, version: null });
262+
}
263+
});
264+
}
213265
} else {
214266
updateValidity({ ...validity, context: contextValidity });
215267
}
@@ -497,7 +549,7 @@ export default function DefaultAPIForm(props) {
497549
}
498550

499551
DefaultAPIForm.defaultProps = {
500-
onValidate: () => {},
552+
onValidate: () => { },
501553
api: {}, // Uncontrolled component
502554
isWebSocket: false,
503555
};

portals/publisher/src/main/webapp/source/src/app/components/ServiceCatalog/CreateApi.jsx

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,85 @@ function CreateApi(props) {
257257
break;
258258
}
259259
case 'context': {
260-
const contextValidity = APIValidation.apiContext.required().validate(value, { abortEarly: false })
260+
let contextValidity = APIValidation.apiContext.required().validate(value, { abortEarly: false })
261261
.error;
262262
const apiContext = value.includes('/') ? value : '/' + value;
263263
if (contextValidity === null) {
264-
APIValidation.apiParameter.validate(field + ':' + apiContext).then((result) => {
265-
if (result.body.list.length > 0 && checkContext(value, result.body.list[0].context)) {
264+
const splitContext = apiContext.split('/');
265+
for (const param of splitContext) {
266+
if (param !== null && param !== '{version}') {
267+
if (param.includes('{version}')) {
268+
contextValidity = APIValidation.apiContextWithoutKeyWords.required()
269+
.validate(value, { abortEarly: false }).error;
270+
updateValidity({
271+
...validity,
272+
context: {
273+
details: [{
274+
message: '{version} cannot exist as a substring in a path param'
275+
}]
276+
},
277+
});
278+
} else if (param.includes('{') || param.includes('}')) {
279+
contextValidity = APIValidation.apiContextWithoutKeyWords.required()
280+
.validate(value, { abortEarly: false }).error;
281+
updateValidity({
282+
...validity,
283+
context: {
284+
details: [{
285+
message: '{ or } cannot exist as a substring in a path param'
286+
}]
287+
},
288+
});
289+
}
290+
}
291+
}
292+
293+
let charCount = 0;
294+
295+
if (contextValidity === null) {
296+
for (const a of apiContext) {
297+
if (a === '(') {
298+
charCount++;
299+
} else if (a === ')') {
300+
charCount--;
301+
}
302+
if (charCount < 0) {
303+
updateValidity({
304+
...validity,
305+
context: {
306+
details: [{
307+
message: 'Parentheses should be balanced in API context'
308+
}]
309+
},
310+
});
311+
}
312+
}
313+
314+
if (charCount > 0) {
266315
updateValidity({
267316
...validity,
268-
context: { details: [{ message: apiContext + ' context already exists' }] },
317+
context: {
318+
details: [{
319+
message: 'Parentheses should be balanced in API context'
320+
}]
321+
},
269322
});
270-
} else {
271-
updateValidity({ ...validity, context: contextValidity, version: null });
272323
}
273-
});
324+
}
325+
326+
if (contextValidity === null && charCount === 0) {
327+
APIValidation.apiParameter.validate(field + ':' + apiContext).then((result) => {
328+
const count = result.body.list.length;
329+
if (count > 0 && checkContext(value, result.body.list)) {
330+
updateValidity({
331+
...validity,
332+
context: { details: [{ message: apiContext + ' context already exists' }] },
333+
});
334+
} else {
335+
updateValidity({ ...validity, context: contextValidity, version: null });
336+
}
337+
});
338+
}
274339
} else {
275340
updateValidity({ ...validity, context: contextValidity });
276341
}

0 commit comments

Comments
 (0)