Delete the name of collections in error messages in Simpl Schema

in the Simpl Schema :
When errors are returned (If the input information is incorrect) , The name of the corresponding collection is also in the error message . LIKE this :

name is required in CollectionName insert

i want this part (in CollectionName insert ) gone

Can you please post your schema?

…and also post the params that fail during the validation

@peterfkruger
this is my Collection And Schema :

import { Mongo } from "meteor/mongo";
import SimpleSchema from 'simpl-schema';

export const ToRegister = new Mongo.Collection('ToRegister');


ToRegisterSchema = new SimpleSchema({
    type : {
        type: Number,
        label:"type",
        required : true,
        max: 2,
        min: 1,
    }
});
 
SimpleSchema.setDefaultMessages({
  initialLanguage: 'en',
  messages: {
    en: {
      required: '{{label}} is required',
      minString: '{{label}} must be at least {{min}} characters',
      maxString: '{{label}} cannot exceed {{max}} characters',
      minNumber: '{{label}} must be at least {{min}}',
      maxNumber: '{{label}} cannot exceed {{max}}',
      minNumberExclusive: '{{label}} must be greater than {{min}}',
      maxNumberExclusive: '{{label}} must be less than {{max}}',
      minDate: '{{label}} must be on or after {{min}}',
      maxDate: '{{label}} cannot be after {{max}}',
      badDate: '{{label}} is not a valid date',
      minCount: 'You must specify at least {{minCount}} values',
      maxCount: 'You cannot specify more than {{maxCount}} values',
      noDecimal: '{{label}} must be an integer',
      notAllowed: '{{value}} is not an allowed value',
      expectedType: '{{label}} must be of type {{dataType}}',
      regEx: function ({
        label,
        type,
        regExp,
      }) {
        // See if there's one where exp matches this expression
        let msgObj;
        if (regExp) {
          msgObj = _.find(regExpMessages, (o) => o.exp && o.exp.toString() === regExp);
        }

        const regExpMessage = msgObj ? msgObj.msg : 'failed regular expression validation';

        return `${label} ${regExpMessage}`;
      },
      keyNotInSchema: '{{name}} is not allowed by the schema',
    },
   
  },
});

ToRegister.attachSchema(ToRegisterSchema);  
 

and i send this param for test result of error message :

RegisterColllection.insert({type : 0});

When you call setDefaultMessages , it simply extends the default defaults. Be sure to call it before you create any of your SimpleSchema instances

From: https://www.npmjs.com/package/simpl-schema#customizing-validation-messages

It looks like you’re creating a SimpleSchema instance first, and only after this you’re calling setDefaultMessages.

@peterfkruger
Can you explain this in an example? I really struggling…

I have been creating new instance of simpl Schema in every collection.
Was i doing it wrong? Did i supposed to create just one?

According to the documentation you should make sure to call SimpleSchema.setDefaultMessages({...}) first (and just a single time, I suppose). Once it’s done, you can go ahead with creating your schemas and attaching them to your collections:

const ToRegisterSchema = new SimpleSchema({...});
ToRegister.attachSchema(ToRegisterSchema);
2 Likes

@saeeed I don’t think there is a need to tag people specifically, I have been around those forums long enough and I can assure you that you will get a response if someone knows, futhermore there are many folks who can step in :slight_smile:

3 Likes

Sorry, i am new here

1 Like

I tried the following code in different ways:
Correctly shows the translation to the detriment of others. But again, it brings the name of the collection .

import { Mongo } from "meteor/mongo";
export const ToRegister = new Mongo.Collection('ToRegister');
import SimpleSchema from 'simpl-schema';
 

SimpleSchema.setDefaultMessages({
  initialLanguage: 'fa',
  messages: {
    en: {
      required: '{{label}} is required',
      minString: '{{label}} must be at least {{min}} characters',
      maxString: '{{label}} cannot exceed {{max}} characters',
      minNumber: '{{label}} must be at least {{min}}',
      maxNumber: '{{label}} cannot exceed {{max}}',
      minNumberExclusive: '{{label}} must be greater than {{min}}',
      maxNumberExclusive: '{{label}} must be less than {{max}}',
      minDate: '{{label}} must be on or after {{min}}',
      maxDate: '{{label}} cannot be after {{max}}',
      badDate: '{{label}} is not a valid date',
      minCount: 'You must specify at least {{minCount}} values',
      maxCount: 'You cannot specify more than {{maxCount}} values',
      noDecimal: '{{label}} must be an integer',
      notAllowed: '{{value}} is not an allowed value',
      expectedType: '{{label}} must be of type {{dataType}}',
      regEx: function ({
        label,
        type,
        regExp,
      }) {
        // See if there's one where exp matches this expression
        let msgObj;
        if (regExp) {
          msgObj = _.find(regExpMessages, (o) => o.exp && o.exp.toString() === regExp);
        }

        const regExpMessage = msgObj ? msgObj.msg : 'failed regular expression validation';

        return `${label} ${regExpMessage}`;
      },
      keyNotInSchema: '{{name}} is not allowed by the schema',
    },
    fa: {
      required: 'فیلد {{label}} اجباری است',
      minString: 'فیلد {{label}} باید حداقل {{min}} نویسه باشد',
      maxString: 'فیلد {{label}} باید حداکثر {{max}} نویسه باشد',
      minNumber: 'فیلد {{label}} باید حداقل مقدار {{min}} دارا باشد',
      maxNumber: 'فیلد {{label}} باید حداکثر مقدار {{max}} دارا باشد',
      minNumberExclusive: '{{label}} باید be greater than {{min}}',
      maxNumberExclusive: '{{label}} باید be less than {{max}}',
      minDate: '{{label}} باید be on or after {{min}}',
      maxDate: '{{label}} cannot be after {{max}}',
      badDate: '{{label}} is not a valid date',
      minCount: 'You باید specify at least {{minCount}} values',
      maxCount: 'You cannot specify more than {{maxCount}} values',
      noDecimal: '{{label}} باید be an integer',
      notAllowed: '{{value}} is not an allowed value',
      expectedType: '{{label}} باید be of type {{dataType}}',
      regEx: function ({
        label,
        type,
        regExp,
      }) {
        // See if there's one where exp matches this expression
        let msgObj;
        if (regExp) {
          msgObj = _.find(regExpMessages, (o) => o.exp && o.exp.toString() === regExp);
        }

        const regExpMessage = msgObj ? msgObj.msg : 'failed regular expression validation';

        return `${label} ${regExpMessage}`;
      },
      keyNotInSchema: '{{name}} is not allowed by the schema',
    },
  },
});

ToRegisterSchema = new SimpleSchema({
  type : {
      type: Number,
      label:"حساب کاربری",
      required : true,
      max: 2,
      min: 1,
  }
});

ToRegister.attachSchema(ToRegisterSchema);

And returns the error text as follows :

"فیلد حساب کاربری باید حداقل مقدار 1 دارا باشد in ToRegister insert"

My problem with this part is and I want it not to be :

in ToRegister insert

I’m afraid I can’t help you any further: I’m neither the author nor the maintainer of SimpleSchema, I’m a humble forum member just like you. All I could do at this point is to dive down into SimpleSchema’s code in order to find out whether this behavior is a bug or a feature. But maybe it is you who should invest this work and report back to us about your findings.

1 Like

So far, you have helped a lot, thank you very much

Please stop tagging me and sending me dms with your questions. I would like to help, but I have too much noise in front of me as it is. This is not the right way to get what you are looking for.

@stolinski
Sorry, I’m new here and not completely familiar.
Maybe I was wrong about method of this forum ( Because really in our country, this is not a violation of the rights of others).
And after my friends warned me, I stopped doing that. And I also corrected this topic.

I would suggest that you do the first schema validation at the method level and not at collection level. This is the best practice, you should validate all user input in the method so that all values you use to process each method has what you are expecting. That will also show a validation error without the collection name.

You could also put the inner code of the method in a try/catch and return whatever error you want.

But ill suggest first to do the method validation.

Hope this helps.

2 Likes

I followed this issue in simpl-schema and came to the conclusion that it is not related to this package at all, but to the meteor-collection2 package.

@aldeed said:

This error comes from the collection2 package, which is maintained in a different repo: https://github.com/Meteor-Community-Packages/meteor-collection2/blob/master/package/collection2/collection2.js#L479
Please submit an issue on that repo instead.

And I raised this issue again in the meteor-collection2 .

In fact, this issue goes back to this line :

Line ERROR

Ok, so we got to the bottom of this: now we know where the unwanted message suffix comes from. However, I suggest to rethink the whole idea of providing translations of error messages related to schema validations – be it collection related or otherwise.

These messages are on the API-level, and therefore their expected audience are developers or admins only. You should never ever show error messages resulting from API calls to your clients: they can’t relate to these messages, and you would only confuse them.

Instead your application is expected to catch all errors resulting from API calls, and provide the users with error messages specifically about the business operation that they initiated. E.g. if a user of your system wants to “register”, and the operation fails on the API level, you would first need to log the actual error message along with its context information on the console or in some error logging system. No translation is required here.

Then you would provide the user with an error message in their language (in this case, Farsi I guess) telling them that

  • unfortunately their registration could not be completed
  • …because there was a system error that your team will fix ASAP (please try again later) etc.

So ultimately you’ll have two separate error messages for two different audiences in two different languages.

2 Likes