Skip to content
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

Custom field name in validations (i18n) #136

Closed
ozziest opened this issue Dec 30, 2022 · 6 comments
Closed

Custom field name in validations (i18n) #136

ozziest opened this issue Dec 30, 2022 · 6 comments
Labels
blocked It is blocked by something else discussion Something that should be discussed need detail Something that needs more detail to work on it version-minor Minor version changes

Comments

@ozziest
Copy link
Member

ozziest commented Dec 30, 2022

Developers should be able to add custom field names to the validation messages.

Motivation

Currently, developers are able to add multiple languages to the API. It works like the following example in form validations;

{
  "errors": {
    "email": ["Das email Feld muss ausgefüllt sein."],
    "name": ["Das name Feld muss ausgefüllt sein."],
    "surname": ["Das surname Feld muss ausgefüllt sein."]
  }
}

As you can see, all validations messages are in German except the field name. Developers should be able to manage this field name for form validations.

Also, we can use these field names in documentation sections.

Definitions

All translations should be defined the under the locales folders by the language code.

For example, locales/en/fields.yaml

email: Email address
phone: Phone
name: Name
surname: Surname

These values should be used by the form validation library like the following example;

let validator = new Validator({ name: '' }, { name: 'required' });
validator.setAttributeNames({ name: 'custom_name' });
if (validator.fails()) {
  validator.errors.first('name'); // "The custom_name field is required."
}

Custom Translations

Developers should be able to use custom translations. fields.yaml file will be used by the Axe API. But the other files should be able to use the following example;

const onBeforeInsert = async ({ t }: IHookParameter) => {
  t("user.gmail_is_not_accepted")
};

gmail_is_not_accepted key should be located under the user.yml locales files.

@ozziest ozziest added need detail Something that needs more detail to work on it discussion Something that should be discussed labels Dec 30, 2022
@ozziest
Copy link
Member Author

ozziest commented Dec 30, 2022

@saracalihan, I would like to hear your opinions about this template. Does it look good enough?

@saracalihan
Copy link
Member

saracalihan commented Jan 1, 2023

@saracalihan, I would like to hear your opinions about this template. Does it look good enough?

It sounds like a very useful feature, but I have some questions.

  • First, can "validator" be used only for validation for localization or is this control HTTP body like Joi? I think the validator can validate both, it seems more powerful.

  • Why do we use YAML instead of JSON file type? Is it easier to read the JSON from the code and for the JS developer? The JSON scenario is more efficient eg I have a custom locale like user.yml and a single field like gmail_is_not_accepted, as a developer I don't want to create a file just for that field, i can import on code directly.

Apart from these questions, it is enough for me for custom localization.

@ozziest
Copy link
Member Author

ozziest commented Jan 1, 2023

@saracalihan, thank you very much for your feedback. 🤗

First, can "validator" be used only for validation for localization or is this control HTTP body like Joi? I think the validator can validate both, it seems more powerful.

In Axe API, we use validatorjs library. The reason we use validatorjs is defining the validation rules is very easy. We just pass the validations rules from the model files to the validatorjs library. In Joi, Axe API has to transform all the validation rules to the Joi functions. About this issue, we should provide field names to the validatorjs to get perfect translated messages.

Why do we use YAML instead of JSON file type? Is it easier to read the JSON from the code and for the JS developer?

I think the YAML is very clear than JSON. But of course, we can use JSON. That's totally fine.

The JSON scenario is more efficient eg I have a custom locale like user.yml and a single field like gmail_is_not_accepted, as a developer I don't want to create a file just for that field, i can import on code directly.

As a developer, you need more fields than one field if you want to support multiple languages at the same time, in reality. Also, developers are not able to manage the validating process. Let's look at the following example;

class Users extends Model {
  get validations(): Record<string, string> {
    return {
      email: "required|email",
      name: "required|max:50",
      surname: "required|max:50",
      age: "max:100",
    };
  }
}

This is the only thing a developer should do to validate data. As you can notice, there is no way to define fields name in different languages. Also, a developer might want to throw some special error messages to the users. For example; You can not use Gmail as a mail address.. Eventually, developers might need to define many things in locales such as field names and custom messages.

Does it make sense to you?

@saracalihan
Copy link
Member

@ozziest You are right about JSON, It becomes difficult to write when dealing with square brackets, quotes.

i get to point, now this flex and usage is okay to me.

@ozziest ozziest added the version-minor Minor version changes label Feb 19, 2023
@ozziest
Copy link
Member Author

ozziest commented Feb 16, 2024

I decided to use json files.

However, we have a problem with the validation library. It sets the language as a global value. Which doesn't allow to clients use different languages at the same time. Check this out

We can't import this feature without replacing the validation library.

@ozziest ozziest added the blocked It is blocked by something else label Feb 16, 2024
@ozziest
Copy link
Member Author

ozziest commented Feb 18, 2024

This feature won't be implemented. Because the current validation library has several issues. (1, 2).

Instead of that, robust-validator library support has been added. And it doesn't need fields names because it allows to handle field name in the frontend side.

So I close this issue.

@ozziest ozziest closed this as completed Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked It is blocked by something else discussion Something that should be discussed need detail Something that needs more detail to work on it version-minor Minor version changes
Projects
None yet
Development

No branches or pull requests

2 participants