4

Has anyone managed to use a common rules system between their frontend and backend?

Similar to this question: Managing client-side and server-side validations in one place, I'm trying to find a way to have consistent business rules applied on the front (JS) and back end (Java). Unlike this question though, I'm not interested in a general cross-platform field validation system (I'm not out to validate the field length).

An example of a business rule can be something like:

If the user's age is under 18, then a parent's email is required.

In this case the nullability of a form field changes based on input from another part of the form.

So that the system would allow:

{"user":"Bob","age":33}

{"user":"Carol","age":15,"parent_email":"foo@bar.com"}

But deny:

{"user":"Alice","age":16}

A rules engine like Drools can create and apply business rules on the backend, but only in a JVM (or CLR).

The default solution seems to be having two different sets of rules which will never stay in sync and cause all kinds of validation problems and edge cases.

I'd love to hear some successes, failures, or better techniques for tackling this problem.

3

This is related to constraint-programming. Prolog is an example of such a system. There exists javascript implementations of prolog http://yieldprolog.sourceforge.net/ . You could then make your prolog program and use it on both the server and client.

Another approach is having all the logic on the server side but expose it as an api which the client could then query using ajax. It would feel almost the same to the user with the exception of a possible small delay.

  • Prolog is definitely a valid rules system, but yieldprolog seems to require setting up the rule in the local language. The rules don't seem like they would be directly transportable between the system. – Sherman Jul 9 '15 at 15:33
  • The ajax method has potential, but as the dataset gets large you would either need to use a lot of bandwidth or have a queue to manage manage missed deltas. It would also impose random latency and close the possibility of working offline. – Sherman Jul 9 '15 at 15:36
  • Regarding yield prolog, I haven't looked more into it, but would it not be possible to host it on the server in node.js? – Esben Skov Pedersen Jul 10 '15 at 6:25
  • Regarding ajax, I'm not sure it is a problem in reality. I've built a prototype before and even for fairly complex constraint problems I was able to implement them in a stateless way(GET requests) so if performance is a problem it is possible to simply throw more hardware at it(cloud). I don't see the need for a queue either, and since I was using GET requests the system was quite cache friendly. – Esben Skov Pedersen Jul 10 '15 at 6:28
1

What about considering a programming model which transpiles to Javascript on the client side? If you live in a Javascript/JVM world then that could be scala.js, if using the clr it could be F# with Fable or F# with websharper. At least the representation of the rule would be in a single source language for both client and server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.