Feedback, ideas, questions, answers on vandium or if you just need some help with AWS lambda. Visit us at http://vandium.io
'use strict';
const vandium = require( 'vandium' );
module.exports.hello = vandium( ( event ) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
return response;
});
Now use vandium.createInstance
.
Example:
'use strict';
const vandium = require( 'vandium' );
exports.handler = vandium.createInstance( {
lambdaProxy: true,
validation: {
schema: {
body: {
firstName: vandium.types.string().min( 1 ).max( 250 ).required(),
lastName: vandium.types.string().min( 1 ).max( 250 ).required(),
age: vandium.types.number().min( 0 ).max( 130 ).required()
}
}
}
})
.handler( function( event /*, context, callback*/ ) {
// log event ?
// console.log( JSON.stringify( event, null, 2 ) );
// echo body portion
return Promise.resolve( event.body );
});
See: https://github.com/vandium-io/vandium-node/blob/master/examples/lambda-proxy/handler.js
'use strict';
const vandium = require( 'vandium' );
exports.handler = vandium.createInstance( {
lambdaProxy: true,
validation: {
schema: {
headers: {
'Access-Control-...': vandium.types.string().required()
...
}
body: {
...
}
}
}
})
.handler( function( event, context, callback ) {
return {
headers: {
headerone: 'results?'
},
body: {}
};
});
Thanks @dc00p .
However I am not able to change the headers on the response from a failed response. Check out the example below:
'use strict'
const vandium = require('vandium')
exports.post = vandium.createInstance({
lambdaProxy: true,
validation: {
schema: {
body: {
requirednumber: vandium.types.number().min(0).max(10).required()
}
}
}
})
.handler(function (event, context, callback) {
return {
headers: {
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': '*'
},
body: {}
}
})
Returns :{
"statusCode": 422,
"headers": {},
"body": "{\"type\":\"ValidationError\",\"message\":\"validation error: child \\\"requirednumber\\\" fails because [\\\"requirednumber\\\" is required]\"}"
}
So vandium basically skips the handler, when the validation fails and therefore does not set the headers?
I'm using Vandium to process POST requests from API Gateway. I have a pretty vanilla implementation so far. The handler works as expected without validation, but as soon as I add the validation step, I get this error:
{
"type": "ValidationError",
"message": "\"value\" must be an object"
}
My code looks more or less like this:
const vandium = require('vandium');
exports.handler = vandium.api()
.protection({mode: 'fail'})
.GET(event => {
// ...
})
.POST({
//validate
body: {
name: vandium.types.string().max(255).required(),
id: vandium.types.number()
}
},
(event, context) => {
return new Promise((resolve, reject) => {
conn.query('select * from user',
(error, results, fields) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
});
});
...and the sample payload I'm using is {name: "Liz", id: "1234"}
. The code does exactly what I want as long as I leave out the validation argument from the .POST()
handler. Looks like a Joi error bubbling up through Vandium. Am I missing something here?
Hi there,
the exact validation error brought me here (nothing in github issues)
I'm using Vandium with Serverless Framework.
According to Serverless Framework, requests templates application/json & application/x-www-form-urlencoded are supported out of the box.
https://serverless.com/framework/docs/providers/aws/events/apigateway#default-request-templates
On my vandium POST method handler,
when i do :
curl -X POST \
http://localhost:3000/users \
-H 'Content-Type: application/json' \
-d '{ "firstName": "first", "lastName": "last", "age": 1 }'
it works as expected
but when i do :
curl -X POST \
http://localhost:3000/users \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'firstName=first&lastName=last&age=1'
it throws validation errors :
{
"type": "ValidationError",
"message": "\"value\" must be an object"
}
here is the handler :
module.exports.post = vandium.api()
.callbackWaitsForEmptyEventLoop(false)
.POST(
{
body: {
firstName: vandium.types.string().min(1).max(250).required(),
lastName: vandium.types.string().min(1).max(250).required(),
age: vandium.types.number().min(1).max(130)
}
},
(event) => { ... });
How could i solve this to support both content-types seamlessly?
the API Gateway event is respectively showingbody: '{ "firstName": "AHDOUHAGIUGAI", "lastName": "Dre", "age": 1 }',
andbody: 'firstName=test&lastName=azhdaa&age=2',
queryParameters
or body
etc
Hello I'm a newbie and was wsorking on serverless project where next step is to use vandium, I have installed vandium but now i'm trying to use it in my existing function but I get this error: Failure: Object(...) is not a function.
below is how i am using it :
import { vandium } from 'vandium';
const action = vandium((event, context, cb) => {
cb(null, getCustomerById(event));
});
......
export {action}
Also i'm getting a warning on import statement over vandium "Could not find a declaration file for module 'vandium'."