You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
@astevelinocermati for consistency reasons and some rules have optional parameter(s)
satpam.rules.requied // undefined T_T
satpam.rules.requied() // Will be caught
satpam.rules.required({
shouldValidate: (input, ruleObject) => input.name === 'ironman' // Will run validation if name is 'ironman'
});
I think it's better if we have our own DSL for rule e.g.
Reasoning behind this is it's easier to read and it's easier to avoid rule typo, because sometimes we do something like this
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
The text was updated successfully, but these errors were encountered: