Skip to content

Instantly share code, notes, and snippets.

@e06widu
Last active June 29, 2020 14:14
Show Gist options
  • Save e06widu/9a862186b9992f08482d5d4d08cae178 to your computer and use it in GitHub Desktop.
Save e06widu/9a862186b9992f08482d5d4d08cae178 to your computer and use it in GitHub Desktop.
Azure function Create user POST body validation
import { AzureFunction, Context, HttpRequest } from '@azure/functions';
import * as Joi from '@hapi/joi';
const httpTrigger: AzureFunction = async (context: Context, req: HttpRequest): Promise<void> => {
context.log('HTTP trigger function processed a request.');
const userSchema = Joi.object({
FirstName: Joi.string().alphanum().max(20).required(),
LastName: Joi.string().alphanum().min(1).max(20),
BirthYear: Joi.number().integer().min(1900).max(2013)
});
try {
const value = await userSchema.validateAsync(req.body);
context.res = {
status: 200,
body: {
message : 'User validated'
}
};
} catch (error) {
context.res = {
status: 400,
body: {
message : error.message
}
};
}
};
export default httpTrigger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment