Closed samuelimoisili closed 1 year ago
@njogz I'm considering using yup
over joi
. yup
seems to be more flexible, it gives you the option to configure the error message.
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();
// yup.json
[
"'name' is required",
"age must be a `number` type, but the final value was: `NaN` (cast from the value `\"wrong_age\"`)."
]
// joi.json
[
{
"message": "\"name\" is required",
"path": [
"name"
],
"type": "any.required",
"context": {
"label": "name",
"key": "name"
}
},
{
"message": "\"age\" must be a number",
"path": [
"age"
],
"type": "number.base",
"context": {
"label": "age",
"value": "wrong_age",
"key": "age"
}
}
]
Even with Joi we can pass in custom error messages e.g.
const schema = joi.object({
name: joi.string().required("'name' is required").error(new Error("name is not valid"),
age: joi.number().required("'age' is required").error(new Error("age is not valid"),
});
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.
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.
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
CHT 4.1.0: Local using docker compose files cht-core.yml and cht-couchdb.yml
interoperability branch: 40-bad-request
@samuelimoisili I understood the desired format of the date was YYYY-MM-DD, and you confirmed.
If you agree, I will send this issue back to In progress
for you to look, and you can also change the message to a clearer one, please.
@lorerod I've updated the error message and regex pattern.
interoperability branch: 40-bad-request
Identifier | bbca4173-b914-43b6-9fcb-437aa8773f38 |
Date of birth | 11 June 1983 |
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 https://github.com/medic/interoperability/issues/19#issuecomment-1443700671