eggjs/egg-validate-schema

Name: egg-validate-schema

Owner: egg

Description: validate by json-schema plugin for egg

Created: 2017-08-03 02:11:21.0

Updated: 2018-04-16 07:32:25.0

Pushed: 2017-08-06 08:27:35.0

Homepage: null

Size: 6

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

egg-validate-schema

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Validate with JSON Schema plugin for egg.

see ajv for more information.

Install
m i egg-validate-schema --save
Usage
app_root}/config/plugin.js
rts.validateSchema = {
ckage: 'egg-validate-schema',

Config

fully support ajv options, see document

app_root}/config/config.{env}.js
rts.validateSchema = {
 allErrors: true,
 v5: true,

Validate Request Body
t jsonSchema = {
ype": "object",
roperties": {
"name": {
  "type": "string"
},
"info": {
  "type": "object"
}

equired": [
"name",
"info"



rts.create = function* () {
 if validate fail will response 422 status code
is.validateBySchema(jsonSchema);
 pass your own data?default use `this.request.body`
 this.validateBySchema(jsonSchema[, your_data]);
 validate pass
is.body = this.request.body;

validate fail response detail?

/1.1 422 Unprocessable Entity


essage": "Validation Failed",
rrors": [
{
  "keyword": "required",
  "dataPath": "",
  "schemaPath": "#/required",
  "params": { missingProperty: 'name' },
  "message": "should have required property 'name'",
}


Questions & Suggestions

Please open an issue here.

License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.