Marco Pestrin

Recently I started using Joi to validate forms on my React application.

In this code snippet I’ll show you how to do a partial and a full validation

The partial one when I’m typing, while the full validation when I click on the submit button.

Below here the syntax of the schema:

Now I necessarily split the schema into more parts because I had to export different models from the file.

To make it more readable, the code becomes like this:

In the frontend it will look like this:

In this case the handleChange function (with the three required parameters) will be called at the onChange event of the form and at the end we will call the validationPayload function.

Into my Errors state I have an object with all errors divided for keys.

Enjoy mates!

--

--

Normally to swap two variables you need a temporary variable because when the first variable is reassigned you lose the value.

We have the syntax available to perform a swap without need an intermediate variable.

Javascript destructuring enable variable swapping without need an intermediate variable.

Cheers mates!

--

--

Often there is a need to compare variable types in javascript

The result is

Apparently there seems to be something wrong because the array is recognized as an object and seems to be no real difference between object and array.

This because in javascript all derived data type is always a type object. Included functions and array.

In case you need to check if it’s an array you can use isArray method of Array.

The result is

otherwise there is a instanceOf operator

and the result will be the same as the previous one.

--

--