Skip to content

Commit

Permalink
Improve doc
Browse files Browse the repository at this point in the history
  • Loading branch information
demsking committed Jun 14, 2017
1 parent 9e036d9 commit 2128b14
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 29 deletions.
71 changes: 56 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ npm install --save jsonschemav
const JsonSchemav = require('jsonschemav')
const jsv = new JsonSchemav()

const schema = { type: 'string', minLength: 6 }
const schema = {
type: 'object',
properties: {
title: { type: 'string', minLength: 6 },
date: { type: 'string', format: 'date', default: 'now()' }
}
}
const data = { title: 'Hello, World!' }
const validator = jsv.compile(schema)

validator
.then((instance) => instance.validate('Hello, World!'))
.then((instance) => instance.validate(datta))
.then((parsedData) => {
// use `parsedData` instead `data`
console.log(parsedData)
// { title: 'Hello, World!',
// date: '2017-06-12T18:49:14.739Z' }
})

validator
Expand Down Expand Up @@ -83,7 +93,7 @@ validator
})
```

## Instance API
## JSV API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

Expand Down Expand Up @@ -142,7 +152,7 @@ jsv.compile(schema)
})
```

Returns **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Returns an interface with the `validate` member
Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** Returns the validation interface on success and an error otherwise

### addCompileStep

Expand Down Expand Up @@ -174,6 +184,7 @@ jsv.compile(schema)
.then((instance) => instance.validate(data))
.then((parsedData) => {
// use `parsedData` instead `data`
console.log(parsedData) // 123
})
.catch((err) => {
// err.errors is a list of parsing error
Expand All @@ -193,10 +204,11 @@ Add a new type to the instance

```javascript
const jsv = new JsonSchemav()
const validateFn = (data) => {
return Number.isInteger(data.value) && /^[01]+$/.test(data.value.toString())
}

jsv.addType('binary', (data) => {
return Number.isInteger(data) && /^[01]+$/.test(data.toString())
})
jsv.addType('binary', validateFn)

const schema = { type: 'binary' }
const data = 1111011
Expand All @@ -205,10 +217,8 @@ const instance = jsv.compile(schema)
jsv.compile(schema)
.then((instance) => instance.validate(data))
.then((parsedData) => {
// use `parsedData` instead `data`
})
.catch((err) => {
// err.errors is a list of parsing error
// use `parsedData` instead `data`
console.log(parsedData) // 1111011
})
```

Expand All @@ -223,14 +233,16 @@ Remove a type from the instance
**Examples**

```javascript
const schema = { type: 'string' }
const jsv = new JsonSchemav()

jsv.removeType('string')

const schema = { type: 'string' }

jsv.validateSchema(schema)
// throw Error: Unknown type 'string'
try {
jsv.validateSchema(schema)
} catch (err) {
console.error(err) // Error: Unknown type 'string'
}
```

### addKeyword
Expand All @@ -243,6 +255,35 @@ Add a new keyword to a type
- `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The name of the new keyword
- `validateFn` **[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** A validate function for the new keyword

**Examples**

```javascript
const jsv = new JsonSchemav()
const validateFn = function (value, data) {
// value is the keyword value
// data.value is the user data
// the function must returns:
// - true: for a success validation
// - false: for a faillure validate
// - an object { message, errors }
// - a Promise for async validation

return new Promise((resolve, reject) => {
//...
})
}

jsv.addKeyword('string', 'provider', validateFn)

// and then
const schema = {
type: 'object',
properties: {
account: { type: 'string', provider: 'twitter' }
}
}
```

### removeKeyword

Remove a keyword from a type
Expand Down
51 changes: 39 additions & 12 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class JsonSchemav {
/**
* Compile a schema
* @param {object} schema - Schema to compile
* @returns {object} Returns an interface with the `validate` member
* @returns {Promise} Returns the validation interface on success and an error otherwise
* @example
* const jsv = new JsonSchemav()
* const schema = {
Expand Down Expand Up @@ -118,6 +118,7 @@ class JsonSchemav {
* .then((instance) => instance.validate(data))
* .then((parsedData) => {
* // use `parsedData` instead `data`
* console.log(parsedData) // 123
* })
* .catch((err) => {
* // err.errors is a list of parsing error
Expand All @@ -141,10 +142,11 @@ class JsonSchemav {
* @param {function} [validateFn] - A validate function for the new type
* @example
* const jsv = new JsonSchemav()
* const validateFn = (data) => {
* return Number.isInteger(data.value) && /^[01]+$/.test(data.value.toString())
* }
*
* jsv.addType('binary', (data) => {
* return Number.isInteger(data) && /^[01]+$/.test(data.toString())
* })
* jsv.addType('binary', validateFn)
*
* const schema = { type: 'binary' }
* const data = 1111011
Expand All @@ -153,10 +155,8 @@ class JsonSchemav {
* jsv.compile(schema)
* .then((instance) => instance.validate(data))
* .then((parsedData) => {
* // use `parsedData` instead `data`
* })
* .catch((err) => {
* // err.errors is a list of parsing error
* // use `parsedData` instead `data`
* console.log(parsedData) // 1111011
* })
*/
addType (name, validateFn) {
Expand All @@ -183,14 +183,16 @@ class JsonSchemav {
* Remove a type from the instance
* @param {string} name - The nema of the type
* @example
* const schema = { type: 'string' }
* const jsv = new JsonSchemav()
*
* jsv.removeType('string')
*
* const schema = { type: 'string' }
*
* jsv.validateSchema(schema)
* // throw Error: Unknown type 'string'
* try {
* jsv.validateSchema(schema)
* } catch (err) {
* console.error(err) // Error: Unknown type 'string'
* }
*/
removeType (name) {
if (this.options.generic.hasOwnProperty(name)) {
Expand All @@ -203,6 +205,31 @@ class JsonSchemav {
* @param {string} type - The name of the type
* @param {string} name - The name of the new keyword
* @param {function} validateFn - A validate function for the new keyword
* @example
* const jsv = new JsonSchemav()
* const validateFn = function (value, data) {
* // value is the keyword value
* // data.value is the user data
* // the function must returns:
* // - true: for a success validation
* // - false: for a faillure validate
* // - an object { message, errors }
* // - a Promise for async validation
*
* return new Promise((resolve, reject) => {
* //...
* })
* }
*
* jsv.addKeyword('string', 'provider', validateFn)
*
* // and then
* const schema = {
* type: 'object',
* properties: {
* account: { type: 'string', provider: 'twitter' }
* }
* }
*/
addKeyword (type, name, validateFn) {
if (!this.options.generic.hasOwnProperty(type)) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonschemav",
"version": "1.0.0",
"version": "1.0.1",
"description": "A simple JSON Schema Validation",
"main": "lib/api.js",
"scripts": {
Expand All @@ -9,7 +9,7 @@
"pretest": "standard",
"test": "mocha --harmony --trace-warnings test/specs test/specs/**/*.spec.js",
"posttest": "npm run doc",
"doc": "documentation readme lib/api.js --shallow --section='Instance API'",
"doc": "documentation readme lib/api.js --shallow --section='JSV API'",
"lint": "standard",
"lintd": "nodemon -x 'npm run lint'",
"testd": "nodemon -x 'mocha --harmony --trace-warnings test/specs test/specs/**/*.spec.js'",
Expand Down

0 comments on commit 2128b14

Please sign in to comment.