9qdvng
Last Updated: February 25, 2016
·
749
· denizac

get just the right level of structure in your schemaless data with tracery

Defining full schemas for your document database? What is this, SQL? (If you've ever tried using JSON Schema or its ilk, I'm sure you know the pain). At Agile Diagnosis, we needed a way to verify data integrity in a way that would allow us to re-use validation logic at different points in the stack and that would let us be as strict or as flexible as we needed. Our approach uses plain old boolean-returning (predicate) functions, in conjunction with a bit of type-and-structure checking sugar we call tracery:

var tracery = require('tracery')

var Tags = tracery([String])

var Movie = tracery({
  title: String,
  director: String,
  year: Number,
  genre: String,
  tags: Tags
})

var Flavor = tracery({
  sour: Boolean,
  bitter: Boolean,
  sweet: Boolean,
  spicy: Boolean
})

var Document = tracery({
  id: String,
  movies: [Movie],
  flavors: tracery.Collection(Flavor)
})

See more at github or grab it from npm install tracery

tracery is free and open source under the MIT license