K-Phoen / rulerz

Powerful implementation of the Specification pattern in PHP
MIT License
872 stars 97 forks source link

Test rules to make sure they are valid #50

Closed mnapoli closed 8 years ago

mnapoli commented 8 years ago

When a UI is responsible for generating some rules, we need to make sure they are valid (that they will execute correctly). Is there a way to "test" that some rules are valid?

What I thought of is trying to compile them with the compiler that Rulerz uses, but:

And finally would it make sense to add that to Rulerz directly?

mnapoli commented 8 years ago

What I did for now and that seems to work:

        try {
            $this->rulerzCompiler->compile($rules, new ArrayVisitor);
        } catch (UnexpectedToken $e) {
            throw new InvalidRules('Invalid promotion rules: ' . $rules, 0, $e);
        }
K-Phoen commented 8 years ago

Is there a way to "test" that some rules are valid?

To consider a rule as "valid", it must:

I've already implemented these verifications in wallabag to check that the tagging rules written by users are valid.

I've written a RuleValidator that compiles the rule, and I use the resulting model to check that all the used variables and operators are allowed. As there is no way to automatically know which variables and operators are allowed, I used a whitelist.

K-Phoen commented 8 years ago

@mnapoli do you need additional input on this?

mnapoli commented 8 years ago

Nope thanks, we've used the trick I shown above for now.