Schema helpers for ClayDB resources
npm install clay-policyclay-policy
==========
[![Build Status][bd_travis_shield_url]][bd_travis_url]
[![npm Version][bd_npm_shield_url]][bd_npm_url]
[![JS Standard][bd_standard_shield_url]][bd_standard_url]
[bd_repo_url]: https://github.com/realglobe-Inc/clay-policy
[bd_travis_url]: http://travis-ci.org/realglobe-Inc/clay-policy
[bd_travis_shield_url]: http://img.shields.io/travis/realglobe-Inc/clay-policy.svg?style=flat
[bd_travis_com_url]: http://travis-ci.com/realglobe-Inc/clay-policy
[bd_travis_com_shield_url]: https://api.travis-ci.com/realglobe-Inc/clay-policy.svg?token=
[bd_license_url]: https://github.com/realglobe-Inc/clay-policy/blob/master/LICENSE
[bd_codeclimate_url]: http://codeclimate.com/github/realglobe-Inc/clay-policy
[bd_codeclimate_shield_url]: http://img.shields.io/codeclimate/github/realglobe-Inc/clay-policy.svg?style=flat
[bd_codeclimate_coverage_shield_url]: http://img.shields.io/codeclimate/coverage/github/realglobe-Inc/clay-policy.svg?style=flat
[bd_gemnasium_url]: https://gemnasium.com/realglobe-Inc/clay-policy
[bd_gemnasium_shield_url]: https://gemnasium.com/realglobe-Inc/clay-policy.svg
[bd_npm_url]: http://www.npmjs.org/package/clay-policy
[bd_npm_shield_url]: http://img.shields.io/npm/v/clay-policy.svg?style=flat
[bd_standard_url]: http://standardjs.com/
[bd_standard_shield_url]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
Schema helpers for ClayDB resources
Table of Contents
----------------
- Installation
- Usage
- Field Restrictions
- API Guide
- Data Types
- License
- Links
Installation
-----
``bash`
$ npm install clay-policy --save
Usage
---------
`javascript
'use strict'
const clayPolicy = require('clay-policy')
const {STRING, DATE} = clayPolicy.DataTypes
async function tryPolicy () {
const policy = clayPolicy({
username: {
type: STRING,
required: true
},
birthday: {
type: DATE
},
rank: {
type: STRING,
oneOf: ['GOLD', 'SLIVER', 'BRONZE']
}
})
const error = policy.validate({
username: 'hoge',
rank: 'SUPER'
})
console.error(error.detail.failures) // -> { rank: { reason: 'enums', expects: [ / ... / ], actual: 'ULTRA' } }
}
tryPolicy().catch((err) => console.error(err))
``
Field Restrictions
-----
| Restriction | Type | Description | default |
| ---------- | ---- | --------- | ------ |
| type | | Type of the field. Possible values are defined as DataTypes in clay-constants package. | |
| oneOf | array | Enum values for the field (Only for STRING type) | |
| required | boolean | Value is required for the field | |
| default | | Default value of the field | |
| unique | boolean | Add unique constraint on the field | |
| uniqueFor | array | Names of attributes to make unique for | |
| minimum | number | Minimum value of the field (Only for NUMBER type) | |
| maximum | number | Maximum value of the field (Only for NUMBER type) | |
| minLength | integer | Minimum length of the field | |
| maxLength | integer | Maximum length of the field | |
| trim | boolean | Trim value. (Only for STRING type) | |
| pattern | string | Restrict value with pattern. (Only for STRING type) | |
| multiple | boolean | Field can contain multiple value. Type restriction will be applied to each items | |
API Guide
-----
+ clay-policy@2.1.13
+ create(args)
+ isPolicy(obj)
+ ClayPolicy
Data Types
-----
Available data types
+ NUMBER
+ STRING
+ BOOLEAN
+ DATE
+ OBJECT
+ NULL
+ REF
+ ENTITY
+ ID
License
-------
This software is released under the Apache-2.0 License.
Links
------
+ [ClayDB][clay_d_b_url]
+ [Realglobe, Inc.][realglobe,_inc__url]
[clay_d_b_url]: https://github.com/realglobe-Inc/claydb
[realglobe,_inc__url]: http://realglobe.jp