Changes for validate required fields,pattern and length and etc#743
Changes for validate required fields,pattern and length and etc#743chavanananada1995 wants to merge 1 commit intoSunbird-Knowlg:release-4.2.0from
Conversation
| "type": "string", | ||
| "format": "date" | ||
| "format": "date", | ||
| "pattern": "^\\d{4}-\\d{2}-\\d{2}$" |
There was a problem hiding this comment.
Since format is already set to date, is setting pattern required ? Is the JSON schema validation not working out of the box ?
There was a problem hiding this comment.
if we set only format then allow minus values in date format that why we add this
@coolbung
| case "minLength": { | ||
| int actual = (int) problem.parametersAsMap().get("actual"); | ||
| int limit = (int) problem.parametersAsMap().get("limit"); | ||
| if( actual < limit) | ||
| { | ||
| return problem.getPointer().replaceAll("/", "") | ||
| + " must be at least "+ problem.parametersAsMap().get("limit") +" characters long"; | ||
| } | ||
| } |
There was a problem hiding this comment.
I don't think we need a custom code to handle minLength. The library which we are using support this. Please validate it again.
There was a problem hiding this comment.
Thank you sir for review code we are check again validate for minlength.
There was a problem hiding this comment.
@maheshkumargangula
Thank you for review and comments. I have checked "minLength" again, but it's not supporting. Here attaching request-response :1) Validating "minLength" without adding custom code for minLength :
POST 'localhost:9000/event/v4/create'
Request Body:
{
"request": {
"event": {
"name": "a",
"status": "Draft",
"code": "EVENT",
"startDate": "2021-08-18",
"endDate": "2021-08-19",
"startTime": "11:20:39+00:00",
"endTime": "10:20:39+00:00",
"registrationEndDate": "2021-08-13",
"eventType": "Online",
"onlineProvider": "BigBlueButton"
}
}
}
Response:
- Event got successfully created without any validation error, even if we have provided event name with single character "a", which is < minLength.
{
"id": "api.event.create",
"ver": "4.0",
"ts": "2021-10-09T12:07:06Z+05:30",
"params": {
"resmsgid": "9a0c332b-363f-4887-977f-ca3ba9019b7e",
"msgid": null,
"err": null,
"status": "successful",
"errmsg": null
},
"responseCode": "OK",
"result": {
"identifier": "do_11338377350280806411",
"node_id": "do_11338377350280806411",
"versionKey": "1633761422263"
}
}
- Validating "minLength" with adding custom code for minLength :
POST 'localhost:9000/event/v4/create'
Request Body:
{
"request": {
"event": {
"name": "a",
"status": "Draft",
"code": "EVENT",
"startDate": "2021-08-18",
"endDate": "2021-08-19",
"startTime": "11:20:39+00:00",
"endTime": "10:20:39+00:00",
"registrationEndDate": "2021-08-13",
"eventType": "Online",
"onlineProvider": "BigBlueButton"
}
}
}
**Response:
- After adding custom code, it supports validation and gives corresponding length error.**
{
"id": "api.event.create",
"ver": "4.0",
"ts": "2021-10-09T11:06:20Z+05:30",
"params": {
"resmsgid": "c288254c-9d24-4ccf-b6fb-492b3191ef2c",
"msgid": null,
"err": "CLIENT_ERROR",
"status": "failed",
"errmsg": "Validation Errors"
},
"responseCode": "CLIENT_ERROR",
"result": {
"messages": [
"name must be at least 5 characters long"
]
}
}
We had also raised ticket about same :
https://github.com/project-sunbird/sunbird-community/discussions/567#discussion-3580472
Validate following cases :
1)Validate field are not accept null and empty values
2) validate length of character
3)Validate pattern like string must be alphanumeric string,accept special character and some cases should not accept special character
4)Validate date should not accept negative values