Skip to content

Bug on Display Error Message on Customize Rules and Customize Message #5

@jeremy-kunzhou

Description

@jeremy-kunzhou

When i tried using customize rules, i guess, i need to add customize message for the new rule i created. Otherwise, when validation failed, the app will show error:

    const { isFieldInError, getErrorsInField, isFormValid } = useValidation({
      fieldsRules: {
        name: { required: true, minlength: 5, maxlength: 30,  cusR: 'aaa'},
      },
      rules: {
        ...defaultRules,
        cusR: (format: string, value) => {
          return value.indexOf(format) > -1;
        }
      },
      state: { name },
    })

TypeError: Cannot read properties of undefined (reading 'replace')

This happened on line 93 on file: services/validator-service.js,
Because no match customized rule name in the default messages map appearantly.

After i add cusomized message (I know i need to add other), the validation failure message will be : "name NaN invalid"

    const { isFieldInError, getErrorsInField, isFormValid } = useValidation({
      fieldsRules: {
        name: { required: true, minlength: 5, maxlength: 30,  cusR: 'aaa'},
      },
      rules: {
        ...defaultRules,
        cusR: (format: string, value) => {
          return value.indexOf(format) > -1;
        }
      },
      messages: {
        ...defaultMessages,
        en: {...defaultMessages.en, cusR: "{0} {1} invalid"}
      },
      state: { name },
    })

I found bug on line 91 on File: services/validator-service.js,

evaluate +value will be 'NaN', if value is undefined.

(function()
{
    'use strict';
    console.log(+ undefined)
}());

// NaN

I think more condition check need to be add on this part.

I don't know whether my explaination makes sense or not. if not , i am sorry about it.

Overall, this is a greate library, I am glad i found it and thank you for your sharing.
If need more information or other query, please let me know. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions