From d779fb72f4cfa352253b4a054d7eb4177eb89678 Mon Sep 17 00:00:00 2001 From: Fabien BERNARD Date: Mon, 25 Oct 2021 15:59:51 +0200 Subject: [PATCH] Add jsDoc parser in example --- example/heros.ts | 39 ++++++++++++++++++++++++++++++ example/heros.zod.ts | 57 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/example/heros.ts b/example/heros.ts index a4ce70c..5cfb90f 100644 --- a/example/heros.ts +++ b/example/heros.ts @@ -94,3 +94,42 @@ export type GetSupermanSkill = ( skillName: string, withKryptonite?: boolean ) => string; + +export interface HeroContact { + /** + * The email of the hero. + * + * @format email + */ + email: string; + + /** + * The name of the hero. + * + * @minLength 2 + * @maxLength 50 + */ + name: string; + + /** + * The phone number of the hero. + * + * @pattern ^([+]?d{1,2}[-s]?|)d{3}[-s]?d{3}[-s]?d{4}$ + */ + phoneNumber: string; + + /** + * Does the hero has super power? + * + * @default true + */ + hasSuperPower?: boolean; + + /** + * The age of the hero + * + * @minimum 0 + * @maximum 500 + */ + age: number; +} diff --git a/example/heros.zod.ts b/example/heros.zod.ts index 9853ee8..0461202 100644 --- a/example/heros.zod.ts +++ b/example/heros.zod.ts @@ -14,8 +14,6 @@ export const enemySchema = z.object({ inPrison: z.boolean(), }); -export const supermanEnemySchema = enemySchema; - export const supermanSchema = z.object({ name: z.union([ z.literal("superman"), @@ -51,15 +49,33 @@ export const killSupermanSchema = z .returns(z.promise(z.boolean())); export const withDefaultsSchema = z.object({ + /** + * @default 42 + */ theAnswerToTheUltimateQuestionOfLife: z.number().default(42), + /** + * @default false + */ isVulnerable: z.boolean().default(false), + /** + * @default clark + */ name: z .union([z.literal("clark"), z.literal("superman"), z.literal("kal-l")]) .default("clark"), + /** + * @default The Answer to the Ultimate Question of Life + */ theMeaningOf42: z .string() .default("The Answer to the Ultimate Question of Life"), + /** + * @default "" + */ emptyString: z.string().optional().default(""), + /** + * @default "true" + */ booleanAsString: z.string().default("true"), }); @@ -77,6 +93,43 @@ export const getSupermanSkillSchema = z .args(z.string(), z.boolean().optional()) .returns(z.string()); +export const heroContactSchema = z.object({ + /** + * The email of the hero. + * + * @format email + */ + email: z.string().email(), + /** + * The name of the hero. + * + * @minLength 2 + * @maxLength 50 + */ + name: z.string().min(2).max(50), + /** + * The phone number of the hero. + * + * @pattern ^([+]?d{1,2}[-s]?|)d{3}[-s]?d{3}[-s]?d{4}$ + */ + phoneNumber: z.string().regex(/^([+]?d{1,2}[-s]?|)d{3}[-s]?d{3}[-s]?d{4}$/), + /** + * Does the hero has super power? + * + * @default true + */ + hasSuperPower: z.boolean().optional().default(true), + /** + * The age of the hero + * + * @minimum 0 + * @maximum 500 + */ + age: z.number().min(0).max(500), +}); + +export const supermanEnemySchema = supermanSchema.shape.enemies.valueSchema; + export const supermanNameSchema = supermanSchema.shape.name; export const supermanInvinciblePowerSchema =