Или о том, как мы подошли к вопросу о валидировании схем
Фото из официального сайта b2broker.com/bbp
import z from 'zod'
const Order = z.object({
orderId: z.string(),
items: z.array(z.object({
name: z.string(),
price: z.number(),
}))
})
type OrderApi = z.infer<typeof Order>
// {orderId: string, items: {name: string, price: number}[] }
const Ajv = require("ajv")
const ajv = new Ajv()
const schema = {
properties: {
foo: {type: "int32"}
},
optionalProperties: {
bar: {type: "string"}
}
}
const data = {foo: 1, bar: "abc"}
const valid = ajv.validate(schema, data)
export class SchemaBuilder {
_schema: any
/**
* returns JSON-schema representation
*/
get schema() {
return this._schema;
}
/**
* Marks your property as nullable (`null`).
*
* Updates `type` property for your schema.
* @example
* const schemaDef = s.string().nullable()
* schemaDef.schema // { type: ['string', 'null'], nullable: true }
*/
nullable(): SchemaBuilder {
// implementation
return this
}
}
// responseModels.ts
import s from 'ajv-ts'
const Order = s.object({
orderId: s.string(),
items: s.array(s.object({
name: s.string(),
price: s.number(),
}))
})
Order.schema
// {type: 'object', properties: { orderId: {type: 'string'} } }
export default {
'mySchema.Enteties.Order': Order
}
import s from 'ajv-ts'
const Order = s.object({
orderId: s.string(),
name: s.string().min(5).max(2) // ! Typescript Error !
})