Joi - Validate input and only then sanitize it - javascript

I'm trying to validate and then sanitize the user input with joi:
Joi.object({
clientName: Joi.string()
.pattern(/^[a-zA-ZñÑáéíóúüÁÉÍÓÚ\s]+$/i)
.replace(' ', '-')
.required()
})
And the string to test is: Jhon Doe
I thought that applying the replace method after the pattern would make Joi to first check against the pattern and then sanitize the input, but when I test this I get a validation error:
"clientName" with value "Jhon-Doe" fails to match the required pattern: /^[a-zA-ZñÑáéíóúüÁÉÍÓÚ\s]+$/i]the string.
It makes sense, that hyphen is not allowed in the pattern but then, how can I first validate and only then apply some sanitizer methods like replace? is it possible to accomplish validation and sanitization in the same statement?
I know I can just create some utility functions to manually sanitize the string after validation but I was thinking in take advantage of the already Joi builtins for this.

Related

Joi validation string exclude uri

I'm trying to validate string parameters and I want to exclude/reject when it matches URI pattern. My current joi is Joi.string().trim().min(2).max(30)
You can use regex to implement your URI pattern and add {invert: true} to regex to reject it.
Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/, {invert: true})

.notRequired on yup string not working if string is empty and still throws an error

I am trying to validate a string that if empty should not be validated but if there is a character then validation should start.
I tried this but to no avail
iban: yup
.string()
.notRequired()
.min(5, 'Minimum of 5')
.max(34, 'Maximum of 34'),
If empty string as in '' it still throws a minimum of 5 error
How can I fix this?
According to the docs on github, it says passing in undefined is acceptable, however the empty string does not equal undefined.
mixed.notRequired(): Schema
Mark the schema as not required. Passing undefined as value will not fail validation.
I have a ticket out with the makers of yup to see if there is a way to do this with the built-in functionality. As a workaround, you can use a regular expression to accomplish this with the match function.
iban: yup.string().matches(/^(|.{5,34})$/, "Between 5-34 characters")
Here's the sandbox: https://codesandbox.io/s/old-sound-rs6il

Yup validation with regex and case insensitive

I have a password reset form where the validation for new password is such that it should match a regex and should not match the userId of the user. The userId check has to be case insensitive.
I tried lowercase method but then the regex validation fails as the value is transformed to lower case before the validation starts.
newPassword: yup
.string()
.notOneOf(
[yup.ref("oldPassword")],
"New password must be different than current password."
)
.matches(REGEX_PASSWORD, "Password does not meet requirements.")
.lowercase()
.notOneOf(
[userId.toLowerCase()],
"New password must not be same as userId"
)
Is there any method which can be used to transform the value only for userId check so that I can perform case insensitive check without worrying about regex fails.
When using Yup if all normal features fail you you can use the .test feature, documented here - https://github.com/jquense/yup#mixedtestname-string-message-string--function-test-function-schema
mixed.test(name: string, message: string | function, test: function): Schema
Adds a test function to the validation chain. Tests are run after any object is cast. Many types have some tests built in, but you can create custom ones easily. In order to allow asynchronous custom validations all (or no) tests are run asynchronously. A consequence of this is that test execution order cannot be guaranteed.
As you note from that quoted text is that test (including those build in by Yup such as the ones you normally use) occur after object is cast, hence your issue. However by making your own test you can cast the value inside and do whatever logic you wish. In your instance it may look something like this:
newPassword: yup
.string()
.notOneOf(
[yup.ref("oldPassword")],
"New password must be different than current password."
)
.matches(REGEX_PASSWORD, "Password does not meet requirements.")
.test(
'uidCheck',
'New password must not be the same as userId',
(item) => item !== undefined ? item.toLowerCase() !== userId.toLowerCase() : true
)

How to check special characters in a string using Yup?

For Example, Bank account number cannot have special characters and I would like to validate the user input using Yup and show error in case of special characters like #,#,$ etc and emoji's.
Main intention here is to know the use of regex with Yup.
Note: I can use Yup.oneOf and restrict the user just to enter the integers.
You can use Yup.matches(/* regex here */, /* error message */)

How to validate an Ethereum address using Joi

A valid ethereum address is "0x" + 40 hex characters.
What would be the Joi definition to validate that ?
you have to use .extend method of joi for creating custom validation. you can have regex based checking in .extend method.
reference https://github.com/hapijs/joi/blob/v14.0.1/API.md#extendextension

Categories

Resources