Closed jy95 closed 7 years ago
A more clear display for error :
The fix I found (cannot make a pull request or commit here) :
function headerHandler (headerParameters, path, method, options) {
// FIX FOR RAML 1.0 : headerParameters is an array like [ "0" : {key: "Content-type", ...} ]
var headerParametersObjForRaml1 = {}
headerParameters.forEach(function(parameter){
if (parameter.hasOwnProperty("key")) {
headerParametersObjForRaml1[parameter.key] = parameter
}
})
var headers = (options.RAMLVersion == 'RAML10') ? extend(DEFAULT_REQUEST_HEADER_PARAMS,lowercaseKeys(headerParametersObjForRaml1)) : extend(DEFAULT_REQUEST_HEADER_PARAMS, lowercaseKeys(headerParameters))
var sanitize = ramlSanitize(headers)
var validate = ramlValidate(headers, options.RAMLVersion)
return function ospreyMethodHeader (req, res, next) {
var headers = sanitize(lowercaseKeys(req.headers))
var result = validate(headers)
if (!result.valid) {
return next(createValidationError(formatRamlErrors(result.errors, 'header')))
}
// Unsets invalid headers. Does not touch `rawHeaders`.
req.headers = options.discardUnknownHeaders === false ? extend(req.headers, headers) : headers
return next()
}
}
Now I must fix issue with bodyHandler ... the raml 1.0 types is like the headParameters issue
bodyHandler : normally finished
function bodyHandler (bodies, path, method, options) {
var bodyMap = {}
var bodiesTypes = {}
bodies.forEach(function(body){
if (body.hasOwnProperty("key")) {
bodiesTypes[body.key] = body
}
})
var universalBodies = (options.RAMLVersion == "RAML10") ? bodiesTypes : bodies;
var types = (options.RAMLVersion == "RAML10") ? Object.keys(bodiesTypes) : Object.keys(bodies)
types.forEach(function (type) {
var handlers = BODY_HANDLERS
.filter(function (handler) {
return is.is(handler[0], type)
})
// Do not parse on wildcards.
if (handlers.length > 1 && !options.parseBodiesOnWildcard) {
return
}
// Attach existing handlers.
handlers.forEach(function (handler) {
var properType = handler[0]
var fn = handler[1]
bodyMap[properType] = fn(universalBodies[type], path, method, options)
})
})
And finally :
function jsonBodyValidationHandler (schema, path, method, options) {
var jsonSchemaCompatibility = require('json-schema-compatibility')
var validate
if (schema.constructor === [].constructor){
// array of properties
//console.log(schema)
var schemaObject = {}
schema.forEach(function(property){
if (property.hasOwnProperty("key")) {
// remove the unuseful properties that can make ramlValidation become crazy
delete property.examples
delete property.typePropertyKind
schemaObject[property.key] = property
}
})
schema = schemaObject
//console.log(schemaObject)
}
var isRAMLType = schema.constructor === {}.constructor
// RAML data types
if (isRAMLType) {
validate = ramlValidate(schema, options.RAMLVersion)
// JSON schema
} else {
try {
schema = JSON.parse(schema);
// Convert draft-03 schema to 04.
if (JSON_SCHEMA_03.test(schema.$schema)) {
schema = jsonSchemaCompatibility.v4(schema)
schema.$schema = 'http://json-schema.org/draft-04/schema'
}
console.log(schema);
validate = ajv.compile(schema)
} catch (err) {
err.message = 'Unable to compile JSON schema for ' + method + ' ' + path + ': ' + err.message
throw err
}
}
The fix on my fork : https://github.com/jy95/osprey-method-handler.
Did you consider adding a PR for that? You only need to use your fork and send a PR from that.
I tried to make a pull request directly with Intellij Idea (but I got error 403) . That is why I forked the project. I will do the PR with github right now. Intellij Idea has removed the indentation so a lot of lines are modified XD
Let's discuss this here: https://github.com/mulesoft/osprey/issues/133#issuecomment-283840943
When I started the server :
When I tried a request , with the content of validation, if it could help
Here is all my raml files :
Api.raml
requests.raml
answers.raml
PS: Sorry for the red color for date : Linguist doesn't seem to like RAML 1.0 : https://help.github.com/articles/creating-and-highlighting-code-blocks/