I use Yup for validation. It's a lot smaller package than Joi. I've used Yup on both front and backend and I usually throw my validations into a shared npm package that both the front end and backend projects can have the same validation library
npm install -S yup
then
import * as yup from 'yup';
let schema = yup.object().shape({
name: yup.string().required(),
age: yup.number().required().positive().integer(),
email: yup.string().email(),
website: yup.string().url(),
createdOn: yup.date().default(function () {
return new Date();
}),
});
// check validity
schema
.isValid({
name: 'jimmy',
age: 24,
})
.then(function (valid) {
valid; // => true
});
From Yup's website:
Yup is a JavaScript schema builder for value parsing and validation.
Define a schema, transform a value to match, validate the shape of an
existing value, or both. Yup schema are extremely expressive and allow
modeling complex, interdependent validations, or value
transformations.
Yup's API is heavily inspired by Joi, but leaner and built with
client-side validation as its primary use-case. Yup separates the
parsing and validating functions into separate steps. cast()
transforms data while validate checks that the input is the correct
shape. Each can be performed together (such as HTML form validation)
or separately (such as deserializing trusted data from APIs).
You will want to create your own validators for special items like password matching etc. This can be done using regex then add the function to Yup like so:
let schema = string().matches(/(hi|bye)/);
Put all your validation functions into your shared NPM package (including your typescript types etc). The cases where your frontend team is out of sync with validations on the backend will now be less of a concern.