0

I am making a schema for users in MongoDB and the problem that i am facing here it is that the web-app will be multi language so meaning this i am doing validation in back end more concretely in user schema, so in required field and unique i put message if something goes wrong and that message i show to the users, so if the user uses the app in different language does it is possible to change from here message and to show him.

This is the schema that i am using:

const UserSchema = new Schema({
  name: {
    type: String,
    trim: true,
    required: "Name is required"
  },
  surname: {
    type: String,
    trim: true,
    required: "Surname is required"
  },
  username: {
    type: String,
    trim: true,
    unique: "Username already exist",
    required: "Username is required"
  },
  email: {
    type: String,
    trim: true,
    unique: "Email already exists",
    match: [/.+\@.+\..+/, "Please fill a valid email address"],
    required: "Email is required"
  },
  password: {
    type: String,
    required: "Password is required"
  },
  role: {
    type: String,
    trim: true,
    required: "Role is required"
  }
});
imcze
  • 389
  • 2
  • 4
  • 13

1 Answers1

0

const validationMessagesTranslation = (field, lang) => {
  const validationMessages = {
    "en": {
      name: "Name is required",
      surname: "Surname is required"
    },
    "fr": {
      name: "Le nom est requis.",
      surname: "Le nom de famille est requis."
    }
  }
  return validationMessages[lang][field];
}

You can create a function that holds an enum with all translations that you need.Pass that function to the required value of your models.

Nicolae Maties
  • 2,476
  • 1
  • 16
  • 26
  • this it works perfectly the other issue that i am facing now is how to pass paramater language when i attempt to save a user for instance here: const newUser = await new User(req.body); newUser.save((err, result) => { if (err) { return res.status(400).json({ error: errorHandler.getErrorMessage(err) }); } res.status(200).json({ message: "Successfully signed up!" }); }); i need here to pass paramater language in order to get the message error in language that i want. thanks for the function. – imcze Mar 28 '19 at 14:38
  • instead of `req.body` passed to new User(); let parameters = Object.create(req.body): parameters.lang = //here pass your language; Than you can read it in your schema model. – Nicolae Maties Mar 28 '19 at 14:43
  • and how to fetch it here that param: name: { type: String, trim: true, required: validationMessagesTranslation("name", "here language paramater") }, – imcze Mar 28 '19 at 15:23
  • Your UserModel can look like a function. const UserModel = (user, lang) => { // now you have the language here, you don't need to use Object.create anymore, just pass it when you are doing `await new User(req,body, lang)` return new Schema({ }) } – Nicolae Maties Mar 28 '19 at 15:47
  • if a change model like this: const UserSchema = lang => { return new Schema({ name: { type: String, trim: true, required: validationMessagesTranslation("name", "fr") } }) } it throws me this error: TypeError: UserSchema.pre is not a function – imcze Mar 28 '19 at 16:03
  • Follow this: https://stackoverflow.com/questions/14959199/passing-model-parameters-into-a-mongoose-model – Nicolae Maties Mar 28 '19 at 16:52
  • the problem is not passing a param but the problem now it appears how to fetch that param here in model schema for instance here: name: { type: String, trim: true, required: validationMessagesTranslation("name", "here fetch param") }, – imcze Mar 28 '19 at 18:58