Skip to content

Transforming a json escaped string #1131

Answered by sinclairzx81
nad-au asked this question in Q&A
Discussion options

You must be logged in to vote

@nad-au Hi,

The following seems to work ok (validating the Data.req part of the payload only)

import { Type as t } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

export const orderItemSchema = t.Object({
  productId: t.String(),
  quantity: t.Number(),
})
const ProductOrder = t.Transform(t.String())
  .Decode(value => Value.Parse(orderItemSchema, JSON.parse(value)))
  .Encode(value => JSON.stringify(value))

export const httpInputBindingSchema = t.Object({
  Url: t.String(),
  Method: t.Union([t.Literal('GET'), t.Literal('POST')]),
  Query: t.Record(t.String(), t.String()),
  Headers: t.Record(t.String(), t.Array(t.String())),
  Params: t.Record(t.String(), t.St…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@nad-au
Comment options

Answer selected by nad-au
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants