Last active
June 29, 2020 14:14
-
-
Save e06widu/9a862186b9992f08482d5d4d08cae178 to your computer and use it in GitHub Desktop.
Azure function Create user POST body validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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