adonisjs / validator

Schema based validator for AdonisJS
MIT License
116 stars 39 forks source link

Why it throw an exception when i send empty string #163

Closed lu7766lu7766 closed 1 year ago

lu7766lu7766 commented 1 year ago

Dear maintainers,

I found an issue while using AdonisJS validation with schema.string(). When attempting to validate an empty string, an exception is thrown instead of properly validating the input. This behavior is inconsistent with the intended functionality of the validation system.

Here is the code snippet I'm using:

name: schema.string()

When the data object contains an empty string for the name field, validation fails and throws the following exception:

ValidationException {
  errors: [
    {
      rule: 'required',
      field: 'name',
      message: 'required validation failed'
    }
  ]
}

But i think "" is valid string too or how could i make it right, i want the name required and can be empty, i had try nullable, optional, nullableAndOptional, these all not what i want.

It would be great if you could take a look at this issue and provide a fix or workaround. Thank you for your hard work on this project.

Package version

 "@adonisjs/core": "^5.9.0",
thetutlage commented 1 year ago

The string validation has not failed. It is the required validation that has failed and an empty string has no value.

The behavior is very much intended and we will not change it. If you are trying to achieve some workflow with an empty string, then please explain that as a discussion thread