All Projects → magna25 → Jebena

magna25 / Jebena

Lightweight JSON validation library

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Jebena

Express Joi Validation
validate express application inputs and parameters using joi
Stars: ✭ 70 (+25%)
Mutual labels:  express, express-middleware
Express Basic Auth
Plug & play basic auth middleware for express
Stars: ✭ 241 (+330.36%)
Mutual labels:  express, express-middleware
Join Io
join files on a fly to reduce requests count
Stars: ✭ 80 (+42.86%)
Mutual labels:  express, express-middleware
Webauthn
W3C Web Authentication API Relying Party for Node.js and Express
Stars: ✭ 61 (+8.93%)
Mutual labels:  express, express-middleware
Express Openapi Validator
🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification
Stars: ✭ 436 (+678.57%)
Mutual labels:  express, express-middleware
Vue Shoppingcart
ShoppingCart (Ecommerce) 🛒 Application using Vuejs, + Node.js + Express + MongoDB 🚀🤘
Stars: ✭ 141 (+151.79%)
Mutual labels:  express, express-middleware
Host Validation
Express.js middleware for "Host" and "Referer" header validation to protect against DNS rebinding attacks.
Stars: ✭ 183 (+226.79%)
Mutual labels:  express, express-middleware
Swagger Express Ts
Generate and serve swagger.json
Stars: ✭ 102 (+82.14%)
Mutual labels:  json, express
Express Mysql Session
A MySQL session store for the express framework in node
Stars: ✭ 268 (+378.57%)
Mutual labels:  express, express-middleware
Zoya
Truly highly composable logging utility
Stars: ✭ 116 (+107.14%)
Mutual labels:  json, express
Node Quick Mock
🌞 基于Express的mock接口平台
Stars: ✭ 33 (-41.07%)
Mutual labels:  json, express
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+1608.93%)
Mutual labels:  json, express
Celebrate
A joi validation middleware for Express.
Stars: ✭ 1,041 (+1758.93%)
Mutual labels:  express, express-middleware
Graphql Upload
Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
Stars: ✭ 1,071 (+1812.5%)
Mutual labels:  express
React Cute
Render cute JSON in React
Stars: ✭ 55 (-1.79%)
Mutual labels:  json
Jsoncrush
Compress JSON into URL friendly strings
Stars: ✭ 1,071 (+1812.5%)
Mutual labels:  json
Saw
Fast, multi-purpose tool for AWS CloudWatch Logs
Stars: ✭ 1,071 (+1812.5%)
Mutual labels:  json
Gulp Json Editor
A gulp plugin to edit JSON objects
Stars: ✭ 55 (-1.79%)
Mutual labels:  json
Easyjson
Provides an unified JSON access API, you can adapter any JSON library to Gson, Jackson, FastJson with easyjson。 提供了一个JSON门面库,就像slf4j一样。easyjson本身不做json的操作,完全依赖于底层实现库。可以直接使用Easyjson的API,底层的JSON库随时可切换。也可以使用其中某个json的API,然后通过easyjson适配给其他的json库
Stars: ✭ 54 (-3.57%)
Mutual labels:  json
Express Fileupload
Simple express file upload middleware that wraps around busboy
Stars: ✭ 1,069 (+1808.93%)
Mutual labels:  express-middleware

alt text


CI License: ISC npm version

Lightweight ES6 promise based JSON validation library with 0 dependencies

Heavily inspired by https://github.com/lperrin/paperwork

Installation

npm i jebena

Usage

import jebena from 'jebena'


const spec = {
    name: String,
    age: Number,
    "phone?": Number // ? = optional prop
}

const data = {
    name: "Foo Bar",
    age: 34
}

jebena(spec, data)
.then( res => {
    // cleaned and validated data
})
.catch(err => {
    // array of errors
})

Express

import {jx, email, runEach} from 'jebena'

const app = express()
app.use(express.json())

const spec = {
    email: email(),
    password: runEach(String, minLen(8)),
}

app.post("/users", jx(spec), () => {
    //req.body is validated and cleaned
})

That's it. jebena returns a 400 response silently if there are any errors. Sample 400 response:

{
  "errors": [
    "password: must be at least 8 characters long"
  ]
}

Options

jebena accepts a third optional argument where you can define your preferences. Below are the defaults

const defaultOps = {
    allowEmpty: false,
    ignoreUnknown: true,
    showOnlyFirstErrorForSameKey: false,
    dataSource: "body" //for express only. either 'body' or 'query'

}

Custom Types

runEach(type1, type2...) validates each type against key value

email() checks if value is email

len(n) checks if value is exactly n characters long

minLen(n) checks if value is at least n characters long

maxLen(n) checks if value is at most n characters long

match(reg) checks if value matches regex

equals(val) checks if value equals given value

oneOf([el, el2, el3...]) checks if value is equal to one of the array elements

ifKey(key, predicate = v => true) runs validation on current key only if the key argument is present in the spec and the predicate returns true

ifKey(key, predicate = v => true).runEach(type1, type2...) validates each type if predicate is true

any() accepts any value but undefined

int() checks if value is integer

pInt() checks if value is positive integer

date(format = "mm/dd/yy") checks if value matches date format. Supports - or / separators and dd/mm/yyyy or mm/dd/yyyy formats



Advanced Usage

Custom Validation

For custom validations simply provide a predicate. If you want to return a custom error message, return [false, <your error message>]

const spec = {
    fullName: (val) => val.split(" ").length == 2 ? true : [false, "invalid name"] 
}

Arrays and nested objects

const spec = {
    links: [], //array of any
    address: {
        street: String,
        city: String,
        state: oneOf(statesList),
        zipCode: runEach(int(),len(5))
    }
    books: [{
        title: String,
        author: String,
        yearPublished: int(),
        someProp: {
            otherProp: Number
        }
    }]
}

Param dependent on other param

There might situations where you want to run a validation on a param only if another param meets certain conditions and for that you can use ifKey()

const spec  = {
    shippingPreference: oneOf(['pickup', 'delivery']),
    deliveryAddress: ifKey('shippingPreference', v => v == 'delivery').runEach({
        street: String,
        city: String,
        state: oneOf(['az','co']),
        zipCode: runEach(int(),len(5))

    })
}

Test

clone repo to your machine and run:

npm run jebena-test

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].