Skip to content

Commit c6039fd

Browse files
committed
wip
1 parent 15da551 commit c6039fd

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

codegen.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,26 @@ generates:
8181
email: email
8282
scalars:
8383
ID: string
84+
example/valibot/schemas.ts:
85+
plugins:
86+
- ./dist/main/index.js:
87+
schema: valibot
88+
importFrom: ../types
89+
withObjectType: true
90+
directives:
91+
# Write directives like
92+
#
93+
# directive:
94+
# arg1: schemaApi
95+
# arg2: ["schemaApi2", "Hello $1"]
96+
#
97+
# See more examples in `./tests/directive.spec.ts`
98+
# https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/main/tests/directive.spec.ts
99+
constraint:
100+
minLength: minLength
101+
# Replace $1 with specified `startsWith` argument value of the constraint directive
102+
startsWith: [regex, /^$1/, message]
103+
format:
104+
email: email
105+
scalars:
106+
ID: string

example/valibot/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Tips for valibot schema
2+
3+
## How to overwrite generated schema?
4+

example/valibot/schemas.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import * as v from 'valibot'
2+
import { AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, HttpInput, HttpMethod, LayoutInput, PageInput, PageType } from '../types'
3+
4+
export const ButtonComponentTypeSchema = v.enum_(ButtonComponentType);
5+
6+
export const EventOptionTypeSchema = v.enum_(EventOptionType);
7+
8+
export const HttpMethodSchema = v.enum_(HttpMethod);
9+
10+
export const PageTypeSchema = v.enum_(PageType);
11+
12+
export function AttributeInputSchema() {
13+
return v.object({
14+
key: v.nullish(v.string()),
15+
val: v.nullish(v.string())
16+
})
17+
}
18+
19+
export function ComponentInputSchema(): v.GenericSchema<ComponentInput> {
20+
return v.object({
21+
child: v.lazy(() => v.nullish(ComponentInputSchema())),
22+
childrens: v.nullish(v.array(v.lazy(() => v.nullable(ComponentInputSchema())))),
23+
event: v.lazy(() => v.nullish(EventInputSchema())),
24+
name: v.string(),
25+
type: ButtonComponentTypeSchema
26+
})
27+
}
28+
29+
export function DropDownComponentInputSchema() {
30+
return v.object({
31+
dropdownComponent: v.lazy(() => v.nullish(ComponentInputSchema())),
32+
getEvent: v.lazy(() => EventInputSchema())
33+
})
34+
}
35+
36+
export function EventArgumentInputSchema(): v.GenericSchema<EventArgumentInput> {
37+
return v.object({
38+
name: v.pipe(v.string(), v.minLength(5)),
39+
value: v.pipe(v.string(), v.regex(/^foo/, "message"))
40+
})
41+
}
42+
43+
type Infer1 = v.InferOutput<ReturnType<typeof EventArgumentInputSchema>>;
44+
45+
const ESchema = v.object({
46+
name: v.pipe(v.string(), v.minLength(5)),
47+
value: v.pipe(v.string(), v.regex(/^foo/, "message"))
48+
});
49+
50+
type Infer2 = v.InferOutput<typeof ESchema>;
51+
52+
// const obj = v.object({ value: v.pipe(v.string(), v.minLength(5)) })
53+
// // const obj = v.pipe(v.string(), v.minLength(5));
54+
// type Infer1 = v.InferOutput<typeof obj>;
55+
56+
// // const str2: v.ObjectSchema<Readonly<{ value: v.SchemaWithPipe<[v.StringSchema<undefined>, v.MinLengthAction<string, 5, undefined>]> }>, undefined> = v.object({ value: v.pipe(v.string(), v.minLength(5)) })
57+
// // const str2: v.ObjectSchema<Readonly<{ value: Omit<v.GenericSchema<string>, '_types' | '_run'> }>, undefined> = v.object({ value: v.pipe(v.string(), v.minLength(5)) })
58+
// // const str2: v.GenericSchema<string> = v.pipe(v.string(), v.minLength(5));
59+
// const obj2: v.GenericSchema<{ value: string }> = v.object({ value: v.pipe(v.string(), v.minLength(5)) })
60+
// type Infer2 = v.InferOutput<typeof obj2>;
61+
62+
// 型が一致しているか確認するためのチェック
63+
export const typeCheck: Infer1 extends Infer2
64+
? Infer2 extends Infer1
65+
? true
66+
: false
67+
: false = true;
68+
69+
70+
export function EventInputSchema() {
71+
return v.object({
72+
arguments: v.array(v.lazy(() => EventArgumentInputSchema())),
73+
options: v.nullish(v.array(EventOptionTypeSchema))
74+
})
75+
}
76+
77+
export function HttpInputSchema() {
78+
return v.object({
79+
method: v.nullish(HttpMethodSchema),
80+
url: v.any()
81+
})
82+
}
83+
84+
export function LayoutInputSchema() {
85+
return v.object({
86+
dropdown: v.lazy(() => v.nullish(DropDownComponentInputSchema()))
87+
})
88+
}
89+
90+
export function PageInputSchema() {
91+
return v.object({
92+
attributes: v.nullish(v.array(v.lazy(() => AttributeInputSchema()))),
93+
date: v.nullish(v.any()),
94+
height: v.number(),
95+
id: v.string(),
96+
layout: v.lazy(() => LayoutInputSchema()),
97+
pageType: PageTypeSchema,
98+
postIDs: v.nullish(v.array(v.string())),
99+
show: v.boolean(),
100+
tags: v.nullish(v.array(v.nullable(v.string()))),
101+
title: v.string(),
102+
width: v.number()
103+
})
104+
}

0 commit comments

Comments
 (0)