-
Notifications
You must be signed in to change notification settings - Fork 11
Open
Labels
Description
I think it's better if we have our own DSL for rule e.g.
const satpam = require('satpam');
// With string (current implementation)
const rules = {
phoneNumber: ['required', 'string'];
title: ['required', {
name: 'memberOf'
params: [['mr', 'mrs']]
}]
};
// Next implementation could be like this
const rules = {
phoneNumber: [
satpam.rules.required(),
satpam.rules.string()
],
title: [
satpam.rules.required(),
satpam.rules.memberOf({
acceptedValues: ['mr', 'mrs']
})
]
};
Reasoning behind this is it's easier to read and it's easier to avoid rule typo, because sometimes we do something like this
const rules = {
phoneNumber: ['require']
};
module.exports = (req, res) => {
// Uh oh rule typo >.<, it won't happen if we have our own DSL,
// would throw error when starting the server
const validationResult = satpam.validate(rules, req.body);
....
};
Bonus point
we can make it backward compatible, we just need to check old rule format and if it matches then parse the rule using the old implementation