0.0.11 • Published 6 years ago

mont-middleware-apply-schema v0.0.11

Weekly downloads
12
License
-
Repository
-
Last release
6 years ago

mont-middleware-apply-schema

Validate mont context args against provided JSON Schemas

Install

npm i mont-middleware-apply-schema

Usage

Contents of colors.json

{ 
  "$id": "colors",
  "type": "object",
  "properties": {
    "body": { "type": "object" }
  },
  "required": [
    "id",
    "body"
  ]
}

Contents of collection.js

const db = require('mont')('localhost/app')

const applySchema = require('mont-middleware-apply-schema')

const colorSchema = require('./colors.json')

const schemas = [ colorSchema ]

const collection = db
  .get('colors')
  .use(applySchema(schemas))

collection
  .insert({ id: '00ff00', body: { name: 'blue' }})
  .then(console.log)

// > 
//  { id: '00ff00', type: 'colors', name: 'blue' }

collection
  .insert({ id: 'ff0000' })
  .catch(console.error)

// > Error 
//  { message: 'should have required property body' }