apeman-app-validate
Version:
apeman app to do schema validation.
227 lines (164 loc) • 5.4 kB
Markdown
apeman-app-validate
==========
<!---
This file is generated by ape-tmpl. Do not update manually.
--->
<!-- Badge Start -->
<a name="badges"></a>
[![Build Status][bd_travis_shield_url]][bd_travis_url]
[![Code Climate][bd_codeclimate_shield_url]][bd_codeclimate_url]
[![Code Coverage][bd_codeclimate_coverage_shield_url]][bd_codeclimate_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/apeman-app-labo/apeman-app-validate
[bd_travis_url]: http://travis-ci.org/apeman-app-labo/apeman-app-validate
[bd_travis_shield_url]: http://img.shields.io/travis/apeman-app-labo/apeman-app-validate.svg?style=flat
[bd_travis_com_url]: http://travis-ci.com/apeman-app-labo/apeman-app-validate
[bd_travis_com_shield_url]: https://api.travis-ci.com/apeman-app-labo/apeman-app-validate.svg?token=
[bd_license_url]: https://github.com/apeman-app-labo/apeman-app-validate/blob/master/LICENSE
[bd_codeclimate_url]: http://codeclimate.com/github/apeman-app-labo/apeman-app-validate
[bd_codeclimate_shield_url]: http://img.shields.io/codeclimate/github/apeman-app-labo/apeman-app-validate.svg?style=flat
[bd_codeclimate_coverage_shield_url]: http://img.shields.io/codeclimate/coverage/github/apeman-app-labo/apeman-app-validate.svg?style=flat
[bd_gemnasium_url]: https://gemnasium.com/apeman-app-labo/apeman-app-validate
[bd_gemnasium_shield_url]: https://gemnasium.com/apeman-app-labo/apeman-app-validate.svg
[bd_npm_url]: http://www.npmjs.org/package/apeman-app-validate
[bd_npm_shield_url]: http://img.shields.io/npm/v/apeman-app-validate.svg?style=flat
[bd_standard_url]: http://standardjs.com/
[bd_standard_shield_url]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
<!-- Badge End -->
<!-- Description Start -->
<a name="description"></a>
apeman app to do schema validation.
<!-- Description End -->
<!-- Overview Start -->
<a name="overview"></a>
<!-- Overview End -->
<!-- Sections Start -->
<a name="sections"></a>
<!-- Section from "doc/guides/01.Installation.md.hbs" Start -->
<a name="section-doc-guides-01-installation-md"></a>
Installation
-----
```bash
$ npm install apeman-app-validate --save
```
<!-- Section from "doc/guides/01.Installation.md.hbs" End -->
<!-- Section from "doc/guides/02.Usage.md.hbs" Start -->
<a name="section-doc-guides-02-usage-md"></a>
Usage
---------
1. Define an app within Apemanfile.js
2. Call the app via apeman app command.
**Apemanfile.js**
```javascript
/** This is an example Apemanfile to use apeman-app-validate */
'use strict'
// Define JSON schema for validation.
const userCreateSchema = {
properties: {
account_name: {
type: 'string',
minLength: 2
}
},
required: [
'account_name'
]
}
module.exports = {
$pkg: { /* ... */ },
$apps: {
// Define your own app.
'my-app-01': {
// Map url and handlers.
'/': [
require('apeman-app-json')(),
require('apeman-app-form')(),
require('apeman-app-route')({
'/user': {
'POST': [
// Add routing function.
// Serve error if validation failed.
require('apeman-app-validate')(userCreateSchema, {
// Options
}),
function doCreateUser (req, res, next) {
/* ... */
}
]
}
})
]
}
}
}
```
Then,
```bash
$ apeman app my-app-01 -p 3000
```
<!-- Section from "doc/guides/02.Usage.md.hbs" End -->
<!-- Section from "doc/guides/03.Signature.md.hbs" Start -->
<a name="section-doc-guides-03-signature-md"></a>
Signature
-------
#### apemanAppValidate(schema, options) -> function
apeman app to do schema validation.
##### Args
| Name | Type | Default | Description |
| --- | ---- | --- | --- |
| schema | object | | A json schema object. |
| options | object | | Optional settings. |
| options.format | string | | Response format like "json", "xml", etc. |
| options.statusCode | string | 400 | Status code for error response. |
| options.prefix | string | ap | Prefix for response header field. |
| options.namespace | string | data | Name space of data in body. |
<!-- Section from "doc/guides/03.Signature.md.hbs" End -->
<!-- Section from "doc/guides/04.Errors.md.hbs" Start -->
<a name="section-doc-guides-04-errors-md"></a>
Errors
------
### Error formats
```
errors.<property>.<details>
```
`property` is name property validation failed.
`details` is an array of error descriptions.
### Error Response Example
```json
{
"errors": {
"accountName": [
{
"reason": "OBJECT_REQUIRED",
"params": {
"key": "account_name"
}
}
],
"password": [
{
"reason": "OBJECT_REQUIRED",
"params": {
"key": "password"
}
}
]
}
}
```
<!-- Section from "doc/guides/04.Errors.md.hbs" End -->
<!-- Sections Start -->
<!-- LICENSE Start -->
<a name="license"></a>
License
-------
This software is released under the [MIT License](https://github.com/apeman-app-labo/apeman-app-validate/blob/master/LICENSE).
<!-- LICENSE End -->
<!-- Links Start -->
<a name="links"></a>
Links
------
+ [apeman](https://github.com/apeman-labo/apeman)
+ [apeman-app](https://github.com/apeman-cmd-labo/apeman-app)
<!-- Links End -->