hyperswitch
Advanced tools
Comparing version 0.9.2 to 0.9.3
@@ -47,4 +47,13 @@ "use strict"; | ||
class Validator { | ||
constructor(parameters) { | ||
constructor(parameters, definitions) { | ||
this._ajv = constructAjv({ verbose: true }); | ||
if (definitions) { | ||
Object.keys(definitions).forEach((schemaName) => { | ||
this._ajv.addSchema( | ||
definitions[schemaName], | ||
`#/definitions/${schemaName}` | ||
); | ||
}); | ||
} | ||
this._paramCoercionFunc = this._createTypeCoercionFunc(parameters.filter((p) => { | ||
@@ -251,3 +260,6 @@ return p.in !== 'formData' && p.in !== 'body' && p.type !== 'string'; | ||
} else { | ||
const validator = new Validator(specInfo.spec.parameters); | ||
const validator = new Validator( | ||
specInfo.spec.parameters, | ||
specInfo.specRoot && specInfo.specRoot.definitions | ||
); | ||
CACHE.set(specInfo.spec, validator); | ||
@@ -254,0 +266,0 @@ validator.validate(req); |
@@ -220,3 +220,4 @@ 'use strict'; | ||
path: match.value.path, | ||
spec: handler.spec | ||
spec: handler.spec, | ||
specRoot: match.value.specRoot | ||
}); | ||
@@ -223,0 +224,0 @@ // This is a hack. Pure P.try get's executed on this tick, but we wanna |
{ | ||
"name": "hyperswitch", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"description": "REST API creation framework", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ "use strict"; | ||
var testValidator = function (req, parameters, example) { | ||
var testValidator = function (req, parameters, example, definitions) { | ||
return validator(null, req, function (hyper, req) { | ||
@@ -18,2 +18,5 @@ if (example) { | ||
parameters: parameters | ||
}, | ||
specRoot: { | ||
definitions: definitions | ||
} | ||
@@ -291,2 +294,33 @@ }); | ||
}); | ||
it('Should support refs to definitions for parameters', () => { | ||
testValidator({ | ||
body: { | ||
test_value: 'four', | ||
int_test_value: 4 | ||
} | ||
}, [ | ||
{ | ||
name: 'bodyParam', | ||
in: 'body', | ||
type: 'object', | ||
schema: { | ||
$ref: '#/definitions/test_schema' | ||
}, | ||
required: 'true' | ||
} | ||
], undefined, { | ||
test_schema: { | ||
type: 'object', | ||
parameters: { | ||
test_value: { | ||
type: 'string' | ||
}, | ||
int_test_value: { | ||
type: 'integer' | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
181691
4206