I'm making a system where users can enter they own expressions. I've searched all of the internet and I can't seem to find a sample of how I could validate the user entered expression before executing it.
Is there a "standard way" or "best practice" to validate user entered expressions or do I have to roll my own validation?
I just want to allow a user to filter a table (array), so the expressions I guess wouldn't be overly complex.
so for an array with headers like array('id', 'firstName', 'lastName', 'docId', 'profit'); I'm expecting expressions like: row.id < 2 and row.profit <= 500
The Symfony/ExpressionLanguage package have a SyntaxError class, and throws this exception if exist error in expression.
Control error:
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\SyntaxError;
$expressionLanguage = new ExpressionLanguage();
try {
$expressionLanguage->evaluate('1 + b.foo');
} catch (SyntaxError $e) {
// Error
}
But, we can not control error by types:
As solution (bad idea), you can control types via text in exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With