Stranger6667/jsonschema-rs

i17n support (custom error messages)

Opened this issue · 2 comments

Thanks for that great crate!

I want to use this crate via Python but this module lacks i17n support so I should try parse English and translate into Japanese.
I believe this support make this more end user friendly.

Hi! Sorry for the delayed response. I like the idea but I don’t know what would be the best way to provide the possibility to choose the language without runtime overhead, so this feature is zero cost

I do also use Json schemas in my PHP projects and I have samthing like that

{
    "type": "object",
    "properties":  
    {
        "partnerid": 
        {
            "type": "string",
            "format": "guid",
            "formatError": "Partner id has wrong format"
        },
        "status": 
        {
            "type": "string",
            "minLength": 1,
            "minLengthError": "Status can not be empty"
        }
}

And my data gateway returns property path and error for it. So the app can use path to find correct UI element and show appropriate error message along to this UI element
The main benefit from custom error messages, instead of standard ugly and non informative (for end user) ... is that I do not need to implement extra functionality that will translate errors from JsonSchema to errors that can informative for end user and for developer only.

PS
For the moment I do use this

pub fn json_get_by_path<'a> (value: &'a serde_json::Value, path: &'a str) -> Option<&'a serde_json::Value> {

    let path_segments: Vec<&str> = path.split('/').collect();
    let mut current_value = value;

    for segment in path_segments {
        match current_value.get(segment) {
            Some(next_value) => {
                current_value = next_value;
            }
            None => {
                return None; // Property not found
            }
        }
    }

    Some(current_value)
}

if let Some(custom_error_message) = json_get_by_path(&schema, format!("{}Error", error.schema_path.to_string().replacen("/", "", 1)).as_str()) {
                error_message = custom_error_message.to_string();
}

It search for custom defined errors in schema. For eg "formatError" or "minLengthError" and etc

So ... you can use multiple json schemas for each language and each language will have custom defined error messages