Skip to content

Commit

Permalink
docs: update to include transpile
Browse files Browse the repository at this point in the history
  • Loading branch information
willfarrell committed Aug 2, 2023
1 parent 11f846b commit e9fa519
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions website/docs/intro/01-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,51 @@ import { transpileSchema } from '@middy/validator/transpile'

// This is your common handler, in no way different than what you are used to doing every day in AWS Lambda
const lambdaHandler = async (event, context) => {
// we don't need to deserialize the body ourself as a middleware will be used to do that
const { creditCardNumber, expiryMonth, expiryYear, cvc, nameOnCard, amount } = event.body
// we don't need to deserialize the body ourself as a middleware will be used to do that
const { creditCardNumber, expiryMonth, expiryYear, cvc, nameOnCard, amount } =
event.body

// do stuff with this data
// ...
// do stuff with this data
// ...

const response = { result: 'success', message: 'payment processed correctly'}
return {statusCode: 200, body: JSON.stringify(response)}
const response = { result: 'success', message: 'payment processed correctly' }
return { statusCode: 200, body: JSON.stringify(response) }
}

// Notice that in the handler you only added base business logic (no deserialization,
// validation or error handler), we will add the rest with middlewares

const schema = {
type: 'object',
properties: {
body: {
type: 'object',
properties: {
creditCardNumber: { type: 'string', minLength: 12, maxLength: 19, pattern: '\\d+' },
expiryMonth: { type: 'integer', minimum: 1, maximum: 12 },
expiryYear: { type: 'integer', minimum: 2017, maximum: 2027 },
cvc: { type: 'string', minLength: 3, maxLength: 4, pattern: '\\d+' },
nameOnCard: { type: 'string' },
amount: { type: 'number' }
},
required: ['creditCardNumber'] // Insert here all required event properties
}
}
type: 'object',
properties: {
body: {
type: 'object',
properties: {
creditCardNumber: {
type: 'string',
minLength: 12,
maxLength: 19,
pattern: '\\d+'
},
expiryMonth: { type: 'integer', minimum: 1, maximum: 12 },
expiryYear: { type: 'integer', minimum: 2017, maximum: 2027 },
cvc: { type: 'string', minLength: 3, maxLength: 4, pattern: '\\d+' },
nameOnCard: { type: 'string' },
amount: { type: 'number' }
},
required: ['creditCardNumber'] // Insert here all required event properties
}
}
}

// Let's "middyfy" our handler, then we will be able to attach middlewares to it
const handler = middy()
.use(jsonBodyParser()) // parses the request body when it's a JSON and converts it to an object
.use(validator({eventSchema: transpile(schema)})) // validates the input
.use(validator({ eventSchema: transpileSchema(schema) })) // validates the input
.use(httpErrorHandler()) // handles common http errors and returns proper responses
.handler(lambdaHandler)

```


## Why?

One of the main strengths of serverless and AWS Lambda is that, from a developer
Expand Down

0 comments on commit e9fa519

Please sign in to comment.