-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make 'Bad Request' body consistent and easy to understand. #40
Comments
@njogz I'm considering using const joi = require("joi");
const yup = require("yup");
const fs = require("fs/promises");
const test = {
age: "wrong_age",
// name is not present.
};
function validateAndSaveYupSchema() {
const schema = yup.object({
name: yup.string().required("'name' is required"),
age: yup.number().required("'age' is required"),
});
try {
schema.validateSync(test, );
} catch (error) {
fs.writeFile("yup.json", JSON.stringify(error.errors, null, 4));
}
}
function validateAndSaveJoiSchema() {
const schema = joi.object({
name: joi.string().required("'name' is required"),
age: joi.number().required("'age' is required"),
});
const error = schema.validate(test, );
fs.writeFile("joi.json", JSON.stringify(error.error.details, null, 4));
}
validateAndSaveJoiSchema();
validateAndSaveYupSchema();
|
Even with Joi we can pass in custom error messages e.g.
Joi seems to have a richer api and is more popular for backend applications so I prefer to keep it over yup. Zod looks interesting since it's typescript first and can work with Joi so that may be worth a look. Looking at the comment from Lorena it seems the main issue is the confusing status code which is a 200 but the body indicates there is an error. So not necessarily the error message from Joi. Let's send back the correct status code if there is an error in validation. |
@njogz Thanks for the Joi error suggestion.
Yeah, but I wanted to standardise the error response along with the request body. I'll use the error syntax you shared for Joi. |
Environment: MacOS 13.1 (22C65); Docker desktop 4.15.0 (93002)l; Docker engine: 20.10.21 Trying to create a patient without required field date of birth ✅Request body:
Response body:
Trying to create a patient with an incorrect value for sex field ✅Request body:
Response body:
Trying to create a patient with numeric _id field ✅Request body:
Response body:
Trying to create a patient with bad format of required field date of birth
|
@lorerod I've updated the error message and regex pattern. |
interoperability branch: Trying to create a patient without required field date of birth ✅Request body:
Response body:
Trying to create a patient with an incorrect value for sex field ✅Request body:
Response body:
Trying to create a patient with numeric _id field ✅Request body:
Response body:
Trying to create a patient with bad format of required field date of birth ✅Request body:
Response body:
Create a patient with all required field ✅Request body:
Response code: 200 ok
This looks good @samuelimoisili thanks! |
When I tried with all the required fields I got the following response:
Request:
POST http://localhost:5001/mediator/patient
{ "_id": "bbca4173-b914-43b6-9fcb-437aa8773f38", "sex":"female", "name":"Lorena"}
Response code: 200 OK
Response Body:
Originally posted by @lorerod in #19 (comment)
The text was updated successfully, but these errors were encountered: