GithubHelp home page GithubHelp logo

anthrax3 / schema Goto Github PK

View Code? Open in Web Editor NEW

This project forked from component/schema

0.0 1.0 0.0 153 KB

A simple, fluent API for generating immutable schemas.

Makefile 4.52% JavaScript 95.48%

schema's Introduction

schema

A simple, fluent API for generating immutable schemas.

Installation

$ component install component/schema

Example

What's nice about your schemas being immutable is that it makes sharing them between different parts of your codebase very simple. For example, you might have a basic user schema:

var Schema = require('schema');

var user = new Schema()
  .string('id', { required: true })
  .string('name')
  .email('email')
  .url('website')
  .date('created', { default: function () { return new Date(); }});

user.toJSON();
{
  "id": {
    "type": "string",
    "required": true,
    "validators": [Function, Function],
  },
  "name": {
    "type": "string",
    "validators": [Function]
  },
  "email": {
    "type": "email",
    "validators": [Function]
  },
  "website": {
    "type": "url",
    "validators": [Function]
  },
  "created": {
    "type": "date",
    "validators": [Function]
  }
}

But then on your servers, you might want to add a few private properties to a user that you don't expose to the public:

var user = require('user-schema');

var backendUser = user
  .number('version', { default: 0 })
  .boolean('admin', { default: false });

And in the browser, you might want to add a few conveniences:

var user = require('user-schema');

var frontendUser = user
  .url('gravatar', { default: {} })
  .object('features', { default: {} });

And in your tests, you might want to have different defaults, so that creating generating fixtures is super simple:

var user = require('user-schema');
var uid = require('uid');
var randomName = require('random-name');
var randomEmail = require('random-email');
var randomUrl = require('random-url');

var fixtureUser = user
  .string('id', { default: uid })
  .string('name', { default: randomName })
  .email('email', { default: randomEmail })
  .url('website', { default: randomWebsite });

API

new Schema()

Create a new Schema.

#(key, settings)

Return a new Schema, adding a property of type by key with optional extra settings. The supported types are:

array
boolean
date
function
number
object
regexp
string
email
url

#add(key, settings)

Return a new Schema, adding a non-typed property by key with optional settings. If you pass a required: true setting, an required validator will be added.

#remove(key)

Return a new Schema, removing a property by key.

#toJSON()

Return a JSON representation of the schema.

schema's People

Contributors

ianstormtaylor avatar juliangruber avatar

Watchers

 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.