Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"winston": "^2.2.0",
"winston-common-sentry": "^0.1.1",
"winston-daily-rotate-file": "^1.2.0",
"winston-logstash": "^0.2.11",
"z-schema": "^3.17.0"
"winston-logstash": "^0.2.11"
},
"devDependencies": {
"babel-cli": "^6.10.1",
Expand Down
25 changes: 3 additions & 22 deletions src/swagger/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,14 @@ exports.getContentType = (headers) => {
return type
}

exports.validateAgainstSchema = (validator, schema, value) => {
schema = _.cloneDeep(schema) // Clone the schema as z-schema alters the provided document

exports.validateAgainstSchema = (validate, value) => {
let response = {
errors: [],
warnings: []
}

if (!validator.validate(value, schema)) {
response.errors = _.map(validator.getLastErrors(), function (err) {
normalizeError(err)

return err
})
if (!validate(value)) {
response.errors = validate.errors
}

return response
Expand Down Expand Up @@ -290,16 +284,3 @@ exports.convertValue = (schema, options, value) => {

return value
}

function normalizeError (obj) {
// Remove superfluous error details
if (_.isUndefined(obj.schemaId)) {
delete obj.schemaId
}

if (obj.inner) {
_.each(obj.inner, function (nObj) {
normalizeError(nObj)
})
}
}
2 changes: 0 additions & 2 deletions src/swagger/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

const parser = require('ruo-swagger-parser')
const parseUrl = require('url').parse
const fs = require('fs')

const _ = require('lodash')
const {resolveRefs: resolve} = require('json-refs')

const Path = require('./path')
const rc = require('../rc')
Expand Down
20 changes: 13 additions & 7 deletions src/swagger/operation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const _ = require('lodash')
const ZSchema = require('./z-schema')
const getAjv = require('./z-schema')

const Parameter = require('./parameter')
const helpers = require('./helpers')

// addtional __data__ field can used with isSwitchOn to return addtional data in unit test
const validator = new ZSchema({assumeAdditional: ['__data__']})
const ajv = getAjv()

class Operation {
constructor (method, definition, parent, pathToDefinition) {
Expand All @@ -20,6 +20,7 @@ class Operation {
})

this.consumes = this.consumes || parent.parent.consumes || []
this.validates = {}
}

validateContentType (contentType, supportedTypes) {
Expand Down Expand Up @@ -92,19 +93,24 @@ class Operation {
return
}

const realStatusCode = res ? String(res.statusCode) : 'default'
const maybeStatusCode = res ? String(res.statusCode) : 'default'
let responseDef = _.find(this.definition.responses, (response, responseCode) => {
return responseCode === realStatusCode
return responseCode === maybeStatusCode
})
responseDef = responseDef || this.definition.responses.default
let realStatusCode = maybeStatusCode
if (!responseDef) {
realStatusCode = 'default'
responseDef = this.definition.responses.default
}
this.validates[realStatusCode] = this.validates[realStatusCode] || ajv.compile(responseDef.schema)

if (responseDef && typeof responseDef === 'object') {
// clone original obj
const data = helpers.prune(JSON.parse(JSON.stringify(obj)))
const valid = validator.validate(data, responseDef.schema)
const valid = this.validates[realStatusCode](data)

if (!valid) {
return validator.getLastErrors()
return this.validates[realStatusCode].errors
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/swagger/operation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('swagger/operation', () => {
}])
expect(errors).to.not.eql(undefined)
expect(errors.length).to.eql(1)
expect(errors[0].code).to.eql('OBJECT_MISSING_REQUIRED_PROPERTY')
expect(errors[0].message).to.eql('should have required property \'name\'')
})

it('should convert value based on schema', () => {
Expand Down
13 changes: 6 additions & 7 deletions src/swagger/parameter-value.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const _ = require('lodash')
const JsonRefs = require('json-refs')
const ZSchema = require('./z-schema')
const getAjv = require('./z-schema')

const helpers = require('./helpers')

const validator = new ZSchema({
breakOnFirstError: false,
ignoreUnknownFormats: true,
reportPathAsArray: true,
assumeAdditional: true
const ajv = getAjv({
allErrors: process.env.NODE_ENV !== 'production',
unknownFormats: true
})

class ParameterValue {
Expand Down Expand Up @@ -86,7 +84,8 @@ class ParameterValue {

if (!skipValidation) {
// Validate against JSON Schema
result = helpers.validateAgainstSchema(validator, parameterObject.schema, value)
this.validate = this.validate || ajv.compile(parameterObject.schema)
result = helpers.validateAgainstSchema(this.validate, value)
}

if (result.errors.length > 0) {
Expand Down
23 changes: 11 additions & 12 deletions src/swagger/z-schema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const _ = require('lodash')
const ZSchema = require('z-schema')
const isNumber = require('lodash.isnumber')
const Ajv = require('ajv')

const validators = {}

Expand All @@ -8,21 +8,20 @@ function returnTrue () {
}

validators.int32 = validators.int64 = function (val) {
// z-schema seems to continue processing the format even when the type is known to be invalid so we must do a type
// check prior to validating this format.
return _.isNumber(val) && val % 1 === 0
return isNumber(val)
}

// These format validators will always return 'true' because they are already type valid and there are no constraints
// on the format that would produce an invalid value.
validators.byte = returnTrue
validators.double = returnTrue
validators.float = returnTrue
validators.password = returnTrue

// Add the custom validators
_.each(validators, function (handler, name) {
ZSchema.registerFormat(name, handler)
})
module.exports = (options) => {
const ajv = new Ajv(options)

module.exports = ZSchema
// Add the custom format validator
Object.keys(validators).forEach(function (name) {
ajv.addFormat(name, validators[name])
})
return ajv
}
12 changes: 6 additions & 6 deletions src/swagger/z-schema.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const {expect} = require('chai')

const ZSchema = require('./z-schema')
const getAjv = require('./z-schema')

const validator = new ZSchema()
const validator = getAjv()

describe('swagger/z-schema', () => {
it('should validate full-date format', () => {
Expand All @@ -22,16 +22,16 @@ describe('swagger/z-schema', () => {

let valid

valid = validator.validate({
valid = validator.validate(schema, {
startedAt: 0,
endedAt: '2016-01-13'
}, schema)
})
expect(valid).to.be.ok

valid = validator.validate({
valid = validator.validate(schema, {
startedAt: NaN,
endedAt: '2016-01-13'
}, schema)
})
expect(valid).to.be.not.ok
})
})
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4088,7 +4088,7 @@ yeast@0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419"

z-schema@^3.17.0, z-schema@^3.19.0:
z-schema@^3.19.0:
version "3.19.0"
resolved "https://registry.yarnpkg.com/z-schema/-/z-schema-3.19.0.tgz#d86e90e5d02113c7b8824ae477dd57208d17a5a8"
dependencies:
Expand Down