This project was motivated by the desire to take a lot of validation and error handling logic out of our services, and put it in a declarative layer.

Law is a middleware layer for tying together:

A set of services

Argument validations and lookups

An access control policy

Law is framework and transport agnostic. Its focus is on enforcing the business rules specific to your application. In our applications, we have connected these services to REST, websockets, and a programmatic API through the use of adapters. I present below an example that should help to tie in as connect middleware. Hopefully there will be enough information here to make this a useful tool for those interested. If you're having trouble getting things working, let me know.

Our approach to policies was inspired by the filter lifecycle from Ruby on Rails. The argument validations take some ideas from Design By Contract, e.g. preconditions. Applying validators to default names was suggested by @JosephJNK. I am interested to see if this will be conducive to creating a 'ubiquitous language' as described by Eric Evans in Domain Driven Design.

The separation of Express and Connect has influenced our decision to do the same. I think that while frameworks can lead to gains in terms of productivity, a library has greater potential for re-use.

Many thanks to @wearefractal (@amurray, @contra), @JosephJNK, and @uberscientist for conversations and collaboration on application design with functional and declarative programming.

Here's some example argument types, and their validations. Law makes these available in the definition of services. You can think of them as the language that a particular set of services share. Whenever these names are used in service arguments, their meanings will be enforced by the rules you set here.

Here's an example policy file. The filters named here are defined as regular services, but they are run in a slightly different context. If they return an error, the filter stack stops and returns it, otherwise their results are merged into the argument stream and passed on to the next service in the stack.

Before you can start using the facilities mentioned above in your app, you'll need to wire some things up. This being a library intended to support a not-yet-released framework, no assumptions are made about the locations of your files. You'll need something like the following to initialize and connect the services when your application starts.

Law provides standard subtypes of Error, enriched using the tea-error
library (https://github.com/qualiancy/tea-error). This means a properties
object can be attached to the Error instance upon construction, as well
as an indicator of the function that the stack trace should start from.

We provide a LawError subtype of Error that acts as a common parent to
all the more specific error subtypes. Extensions to Law should extend this
type to obtain the benefits of tea-error, as well as permitting distinction
between instances of Law errors from application errors.

A service declared an unresolvable dependency category. For example, if the
resolver configuration doesn't define a way to resolve dependencies in a
services category, this error would be thrown. Occurs at setup time, when
resolving dependencies.

This should be considered an alpha release. The API may change. It was developed within an active project with the intent to only build the features which give us a clear advantage. Additional features will be added as required for the parent project.

Discussion/feedback is welcome. You can reach me on twitter @qbitmage.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.