sealcode / sealious

An extensible, declarative node framework
25 stars 2 forks source link

Arguments validation in field types and access strategies #278

Closed adwydman closed 6 years ago

adwydman commented 8 years ago

Consider this piece of code:

function parse_params (params){
    const access_strategies = [];
    for (const i in params){
        access_strategies.push(new Sealious.AccessStrategy(params[i]));
    }
    return access_strategies;
}

module.exports = {
    name: "and",
    item_sensitive: function(params){
        const access_strategies = parse_params(params);
        return Promise.map(access_strategies, function(access_strategy){
            return access_strategy.is_item_sensitive();
        }).reduce((a,b) => a || b);
    },

If params provided in item_sensitive method is, let's say, undefined, parse_params will throw an error while trying to loop through it and this is unsafe. And since we want all chips to export themselves, there should be some kind of validation above them.

adwydman commented 8 years ago

@kuba-orlik

kuba-orlik commented 8 years ago

It makes no sense to instantiate an and access strategy without any params. We should just throw an error in the parse_params method when params are not an array. What do you say?

adwydman commented 8 years ago

I agree, but params can be an object or an array as well. This should be verified.

kuba-orlik commented 8 years ago

Yes, in general, params can be of any type. But in case of the and and or access strategies, params must be arrays, peroid. I guess it would be cool to have a validate_params method that would be a part of the field-type declaration and would validate any given params before passing them to other methods. What do you think?

kuba-orlik commented 6 years ago

Issues moved do Sealcode's phabricator: https://hub.sealcode.org/project/view/11/