All Projects → Kikobeats → Osom

Kikobeats / Osom

Licence: mit
An Awesome [/osom/] Object Data Modeling (Database Agnostic).

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Osom

Datavec
ETL Library for Machine Learning - data pipelines, data munging and wrangling
Stars: ✭ 272 (+300%)
Mutual labels:  schema, transformations
Neo4j Graphql
An optimized neo4j query resolver for Facebook's GraphQL
Stars: ✭ 60 (-11.76%)
Mutual labels:  schema
Graphql Factory
A toolkit for building GraphQL
Stars: ✭ 44 (-35.29%)
Mutual labels:  schema
Sql Boot
Advanced REST-wrapper for your SQL-queries (actually not only SQL)
Stars: ✭ 51 (-25%)
Mutual labels:  schema
Zaml
The Final Form of configuration files
Stars: ✭ 45 (-33.82%)
Mutual labels:  schema
Optimus
🤖 Id obfuscation based on Knuth's multiplicative hashing method for PHP.
Stars: ✭ 1,084 (+1494.12%)
Mutual labels:  transformations
Schemacrawler
Free database schema discovery and comprehension tool
Stars: ✭ 1,021 (+1401.47%)
Mutual labels:  schema
Stetl
Stetl, Streaming ETL, is a lightweight geospatial processing and ETL framework written in Python.
Stars: ✭ 64 (-5.88%)
Mutual labels:  transformations
Govalid
Data validation library for golang. [MIGRATING TO NEW ADDRESS]
Stars: ✭ 59 (-13.24%)
Mutual labels:  schema
Dgman
Dgraph schema manager, with mutate and query helpers
Stars: ✭ 50 (-26.47%)
Mutual labels:  schema
Compojure Api
Sweet web apis with Compojure & Swagger
Stars: ✭ 1,056 (+1452.94%)
Mutual labels:  schema
Beeschema
Binary Schema Library for C#
Stars: ✭ 46 (-32.35%)
Mutual labels:  schema
Squid
🦑 Provides SQL tagged template strings and schema definition functions.
Stars: ✭ 57 (-16.18%)
Mutual labels:  schema
Ha Db
探索高效的SAAS结构,单体应用动态切换数据库。
Stars: ✭ 44 (-35.29%)
Mutual labels:  schema
Autocomplete Json
Atom autocomplete for JSON files
Stars: ✭ 60 (-11.76%)
Mutual labels:  schema
Rdbms To Graphql
A Java CLI program that generates a GraphQL schema from a JDBC data source.
Stars: ✭ 44 (-35.29%)
Mutual labels:  schema
Inicpp
C++ parser of INI files with schema validation.
Stars: ✭ 47 (-30.88%)
Mutual labels:  schema
Metamorph
Morphing mod for Minecraft 1.12.2
Stars: ✭ 52 (-23.53%)
Mutual labels:  transformations
Spark Bigquery
Google BigQuery support for Spark, Structured Streaming, SQL, and DataFrames with easy Databricks integration.
Stars: ✭ 65 (-4.41%)
Mutual labels:  schema
Captionjs
An open-source jQuery plugin to easily and semantically add captions to images.
Stars: ✭ 60 (-11.76%)
Mutual labels:  schema

osom

An Awesome [/osom/] Object Data Modeling. Inspired in mongoose but Database Agnostic.

Last version Build Status Coverage Status Dependency status Dev Dependencies Status NPM Status Donate

Installation

$ npm install osom --save

Preview

var osom = require('osom')

function trim (str) {
  return str.trim()
}

function isValidTitle (str) {
  return str.length > 0
}

// setup your schema
var schema = {
  title: {
    type: String,
    validate: isValidTitle,
    transform: [trim]
  },
  category: String,
  type: String,
  source: String,
  link: String,
  createdAt: String,
  updatedAt: String
}

// create validator based on schemas
var validator = osom(schema)

// validate it!
validator({ title: '  23  ' }) // => {title: '23'}

Usage

osom(schema, [global])

where:

  • schema: It represents a set of rules (one per each key) that will be used for validate an object.
  • global (optional): It brings you the possibility to declare global rules definition as helper to avoid write repetitive code.

After that, you will have a validator function that you can invoke passing the object to be validate.

Schema

Simple

The most common use case is validate the type of something.

If you are only interested in the type, you can provide a simple schema like:

var simpleSchema = {
  age: Number
}

Where key is the name of the rule and value the type of it.

Advanced

The basic mode is a simplification of the advanced mode for the most common use case.

While in basic mode only is possible setup type, in advanced mode you can setup more things providing a configurable object.

Each key of the object represent a rule . It's possible setup different things in the same rule.

Defining Rules

type

Type: function

As in basic mode, it specifies the type of the output value:

var schema = {
  age: {
    type: Number
  }
}

Internally it uses chaste. This makes easy casting compatible types:

var schema = {
  age: {
    type: Number
  }
}

var validator = osom(schema)
validator({ age: '23' }) // => {age: 23}

casting

Type: boolean
Default: true

It enable/disable type casting. An TypeError will be throwed under different type evaluation.

var schema = {
  age: {
    type: String,
    casting: false
  }
}

var validator = osom(schema)
validator({ age: '23' }) // => TypeError("Expected a {string} for 'age'.")

required

Type: boolean|string
Default: false

It marks a rule as required field and throws TypeError if value for the field is not present.

Additionally is possible provide a custom error message. For do it, pass an String.

var schema = {
  age: {
    type: String,
    required: 'sorry but you must provide an age.'
  }
}

var validator = osom(schema)
validator({}) // => TypeError("sorry but you must provide an age")

default

Type: string|object|number|boolean|function
Default: null

It sets a default value if nill value as input is provided.

Additionally you can provide a function for set a dynamic value:

var schema = {
  age: {
    type: Number, default: function () { return 23 }
  }
}

var validator = osom(schema)
validator({}) // => { age: 23 }

transform

Type: function|array
Default: []

It transforms the input value.

The Methods provided in the array are applied as pipeline (the input of the second is the output of the first).

function trim (str) {
  return str.trim()
}

var schema = {
  age: {
    type: String,
    transform: [trim]
  }
}

var validator = osom(schema)
validator({ age: '    23   ' }) // => { age: '23' }

validate

Type: function|object
Default: null

It set up a function that will be exec to validate the input value. If it fails, it throws TypeError.

var schema = {
  age: {
    type: String,
    validate: function (v) {
      return v === '23'
    }
  }
}

var validator = osom(schema)
validator({ age: 25 }) // => TypeError("Fail '25' validation for 'age'.")

Providing a object brings you the possibility set up a custom error message:

var schema = {
  age: {
    type: String,
    validate: {
      validator: function (v) {
        return v === '23'
      },
      message: 'expected a millenial value instead of %s!'
    }
  }
}

var validator = osom(schema)
validator({ age: 25 }) // => TypeError("expected a millenial value instead of 25!")

Defining Global Rules

While is possible provide specific setup per each rule, also is possible provide them as global to apply to all rules.

This minimizes the schemas definitions.

function trim (str) {
  return str.trim()
}

var schema = {
  age: {
    type: String
  }
}

var globalFields = {
  transform: [trim]
}

var validator = osom(schema, globalFields)
validator({ age: '  23  ' }) // => {age: '23'}

No problem if later you need to avoid it for a specific case.

function trim (str) {
  return str.trim()
}

var schema = {
  age: {
    type: String,
    transform: []
  }
}

var globalFields = {
  transform: [trim]
}

var validator = osom(schema, globalFields)
validator({ age: '  23  ' }) // => {age: '  23  '}

Tips

Working with async code

This library works synchronously.

However, you can use it comfortably in a async workflow transforming the interface into a callback/promise style.

For example, consider use async#asyncify for do it. we could have a schema.js file like:

var schema = osom({
  title: {
    type: String,
    validate: isValidTitle,
    transform: [trim]
  },
  category: String,
  type: String,
  source: String,
  link: String,
  createdAt: String,
  updatedAt: String
})

module.exports = async.asyncify(schema)
module.exports.sync = schema

Then you only need use it into a async workflow:

var schema = require('./schema')
schema(data, function (validationError, instance) {
  /** do something */
})

Be careful: this transformation doesn't mean that the function works now asynchronously; Just is converting try-catch interface into callback(err, data).

License

MIT © Kiko Beats

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].