
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
@kilix/functional-validation
Advanced tools
Lightweight and customisable validation, built through composition
Lightweight and customisable validation, built through composition.
Validation is complicated in JavaScript. The most common solution is to use a validator object-shaped, à la joy or yup. However, I find it mostly made to make it straightforward to write simple validation, more complicated ones need to use specific APIs. Since validation can also be seen as a list of validations model -> ?error
, you don't need a highly abstract API to be able to run any kind of validation. Moreover, with little overhead, by composing functions, you can compose validations: a validator that validates an order can use another one that validates the validity of a product!
There are two core functions, validateModel: Array<Validation> => Model => Array<error>
, where Validation: Model => Array<error> | ?error
and createValidation : params => Model => ?error
. I'll come back later on the createValidation
params, but you can observe that both the functions returned by validateModel
and createValidation
can used to create validations used by validateModel
. Moreover, createValidation
doesn't return anything really fancy, it just takes a given model, and can return an error. So you're always free to skip it if one of your validations is too complicated to work with out API (or if it makes it clearer to work without). This lets you use the same pattern (model => ?error
, or model => Array<error>
) for any kind of validation.
Here is a more concrete example:
const validatorNewUser = validateModel([
// This returns an error {field: 'age', error: 'underaged'} if model.age < 18
createValidation(
'age', // used to be able to set the field value of the error
model => model.age, // returns the part of the model that will be checked
// checks the relevant part of the model
// returns the name of the error as a string if there is one
age => age >= 18 ? null : 'underaged',
),
]);
You can argue that using createValidation
is pretty verbose (and you'd be right), however, you need to keep in mind that this library aiming for a more FPesque code style, model => model.age
might already be declared somewhere else, and that checking if an age is underaged can be useful to validate other models. Moreover, we do provide you with several generic value validators.
Lastly, createValidation
is the low-level validator, we have some higher-order ones, to avoid too-verbose validations:
const validateAge = age => age >= 18 ? null : 'underaged';
// This is equivalent to the previous one, since it's that common to test a field without needing
// to use another key as the error name
createSimpleValidation(validateAge,'age');
createNestedValidation(validateAge,['person','age']);
// is equivalent to
createValidation('person.age', model => model.person.age, validateAge);
runConditionValidation(
model => model.age < 18,
[createSimpleValidation(authorised => authorised ? null : 'not','authorisedByParents')]
);
// is equivalent to
model => model.age < 18
? model.authorisedByParents ? null : 'not'
: null;
FAQs
Lightweight and customisable validation, built through composition
The npm package @kilix/functional-validation receives a total of 9 weekly downloads. As such, @kilix/functional-validation popularity was classified as not popular.
We found that @kilix/functional-validation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.