Giter VIP home page Giter VIP logo

formal-mongoose's Introduction

Formal-mongoose Build Status Coverage Status Dependency Status Code Climate

Formal + Mongoose = DRY! Simple solution to define a form with validation (and more) from a mongoose schema.

Example

var Form = require('formal-mongoose'),
    model = mongoose.model('User');

var form = new Form(model, ['name.*', 'email']);

form.field({
  tos: {
    type: Boolean,
    default: false,
    validate: function(val) {
      return val === true;
    }
  }
});

form.set({
  name: {family: 'Martinez'},
  'name.first': 'José Luis Chavez',
  age: 12,
  tos: true
});

form.validate(function (err) {
  console.log(err); // missing required email, age to low
  console.log(form.get('name.first.0')); // José
  console.log(form.export());
});

Install

npm install formal-mongoose --save

Summary

Extend formal to provides fast and easy way to define a form from an existing schema. The best way to don't repeat yourself!

API

For the inherited prototype see the Formal API.

new Form(schema:Object|String, fields:Array, options:Object):instance

schema mongoose schema or model
fields array of strings path to import from the mongoose schema
options object of options identical to Formal

var Form = require('formal-mongoose');
var form = new Form(mongoose.model('User'), ['username', 'pasword']);

For connect and express the alternative factory method can be used as a quick helper to create a new instance and return form.middleware() to monkey patch the request and response object.

app.post('/url',
  // sames as (new Form({...})).middleware()
  form(schema, ['username', 'password']),
  function (req, res) {
    console.log(req.form.data);
    console.log(res.locals.form.username.value);
  }
);

form.addPath(path:String):void

Add a path from the schema.

Notes:

Allows path to end with a wildcard * to import direct children example:

// to import name.firstname and name.lastname
form.addPath('name.*')

Test

npm test
Mocha Coverage npm run-script coverage
On coveralls.io

All tests are in Coffee-script, hence easy to read! Provides a great way to understand the API ;)

LICENSE

MIT

formal-mongoose's People

Contributors

nrako avatar

Watchers

Navid Nikpour avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.