-
-
Notifications
You must be signed in to change notification settings - Fork 51
feat: minimal implementation to support valibot #667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
28d0296
chore: add valibot to devDependencies
MH4GF fb5c8e0
feat: support non-null and defined
MH4GF 6cead63
feat: support nullish
MH4GF df63604
feat: support array
MH4GF 657ee60
feat: support ref input
MH4GF 0e81efc
feat: support enum
MH4GF 998de88
test: add test for camelcase
MH4GF 1bc9066
test: add test for scalars
MH4GF 2484a06
test: add test for importFrom
MH4GF 5a567db
test: add test with importFrom & useTypeImports
MH4GF c8e7c07
feat: support for enumsAsTypes
MH4GF ac18b28
feat: support scalarSchemas
MH4GF 2731e1f
refactor: change buildApiFromDirectiveArguments return type to string[]
MH4GF 3c3f73a
feat: adds buildApiForValibot()
MH4GF 48b2aa7
feat: support directives
MH4GF 7aa54a9
feat: support object type and union type
MH4GF 63c56af
feat: support interface type
MH4GF 9f0a5ec
test: add generate codes and type-check script
MH4GF c5a987b
Merge remote-tracking branch 'origin' into support-valibot-2
MH4GF 75c4fbe
chore: remove duplicated test case
MH4GF b9cb35b
refactor: add _buildApiFromDirectiveArguments()
MH4GF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import * as v from 'valibot' | ||
import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, Namer, PageInput, PageType, User } from '../types' | ||
|
||
export const ButtonComponentTypeSchema = v.enum_(ButtonComponentType); | ||
|
||
export const EventOptionTypeSchema = v.enum_(EventOptionType); | ||
|
||
export const HttpMethodSchema = v.enum_(HttpMethod); | ||
|
||
export const PageTypeSchema = v.enum_(PageType); | ||
|
||
export function AdminSchema(): v.GenericSchema<Admin> { | ||
return v.object({ | ||
__typename: v.optional(v.literal('Admin')), | ||
lastModifiedAt: v.nullish(v.any()) | ||
}) | ||
} | ||
|
||
export function AttributeInputSchema(): v.GenericSchema<AttributeInput> { | ||
return v.object({ | ||
key: v.nullish(v.string()), | ||
val: v.nullish(v.string()) | ||
}) | ||
} | ||
|
||
export function ComponentInputSchema(): v.GenericSchema<ComponentInput> { | ||
return v.object({ | ||
child: v.lazy(() => v.nullish(ComponentInputSchema())), | ||
childrens: v.nullish(v.array(v.lazy(() => v.nullable(ComponentInputSchema())))), | ||
event: v.lazy(() => v.nullish(EventInputSchema())), | ||
name: v.string(), | ||
type: ButtonComponentTypeSchema | ||
}) | ||
} | ||
|
||
export function DropDownComponentInputSchema(): v.GenericSchema<DropDownComponentInput> { | ||
return v.object({ | ||
dropdownComponent: v.lazy(() => v.nullish(ComponentInputSchema())), | ||
getEvent: v.lazy(() => EventInputSchema()) | ||
}) | ||
} | ||
|
||
export function EventArgumentInputSchema(): v.GenericSchema<EventArgumentInput> { | ||
return v.object({ | ||
name: v.pipe(v.string(), v.minLength(5)), | ||
value: v.pipe(v.string(), v.regex(/^foo/, "message")) | ||
}) | ||
} | ||
|
||
export function EventInputSchema(): v.GenericSchema<EventInput> { | ||
return v.object({ | ||
arguments: v.array(v.lazy(() => EventArgumentInputSchema())), | ||
options: v.nullish(v.array(EventOptionTypeSchema)) | ||
}) | ||
} | ||
|
||
export function GuestSchema(): v.GenericSchema<Guest> { | ||
return v.object({ | ||
__typename: v.optional(v.literal('Guest')), | ||
lastLoggedIn: v.nullish(v.any()) | ||
}) | ||
} | ||
|
||
export function HttpInputSchema(): v.GenericSchema<HttpInput> { | ||
return v.object({ | ||
method: v.nullish(HttpMethodSchema), | ||
url: v.any() | ||
}) | ||
} | ||
|
||
export function LayoutInputSchema(): v.GenericSchema<LayoutInput> { | ||
return v.object({ | ||
dropdown: v.lazy(() => v.nullish(DropDownComponentInputSchema())) | ||
}) | ||
} | ||
|
||
export function MyTypeSchema(): v.GenericSchema<MyType> { | ||
return v.object({ | ||
__typename: v.optional(v.literal('MyType')), | ||
foo: v.nullish(v.string()) | ||
}) | ||
} | ||
|
||
export function MyTypeFooArgsSchema(): v.GenericSchema<MyTypeFooArgs> { | ||
return v.object({ | ||
a: v.nullish(v.string()), | ||
b: v.number(), | ||
c: v.nullish(v.boolean()), | ||
d: v.number() | ||
}) | ||
} | ||
|
||
export function NamerSchema(): v.GenericSchema<Namer> { | ||
return v.object({ | ||
name: v.nullish(v.string()) | ||
}) | ||
} | ||
|
||
export function PageInputSchema(): v.GenericSchema<PageInput> { | ||
return v.object({ | ||
attributes: v.nullish(v.array(v.lazy(() => AttributeInputSchema()))), | ||
date: v.nullish(v.any()), | ||
height: v.number(), | ||
id: v.string(), | ||
layout: v.lazy(() => LayoutInputSchema()), | ||
pageType: PageTypeSchema, | ||
postIDs: v.nullish(v.array(v.string())), | ||
show: v.boolean(), | ||
tags: v.nullish(v.array(v.nullable(v.string()))), | ||
title: v.string(), | ||
width: v.number() | ||
}) | ||
} | ||
|
||
export function UserSchema(): v.GenericSchema<User> { | ||
return v.object({ | ||
__typename: v.optional(v.literal('User')), | ||
createdAt: v.nullish(v.any()), | ||
email: v.nullish(v.string()), | ||
id: v.nullish(v.string()), | ||
kind: v.nullish(UserKindSchema()), | ||
name: v.nullish(v.string()), | ||
password: v.nullish(v.string()), | ||
updatedAt: v.nullish(v.any()) | ||
}) | ||
} | ||
|
||
export function UserKindSchema() { | ||
return v.union([AdminSchema(), GuestSchema()]) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,39 @@ export function buildApi(config: FormattedDirectiveConfig, directives: ReadonlyA | |
.join('') | ||
} | ||
|
||
// This function generates `[v.minLength(100), v.email()]` | ||
// NOTE: valibot's API is not a method chain, so it is prepared separately from buildApi. | ||
// | ||
Comment on lines
+123
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I understand 👍 |
||
// config | ||
// { | ||
// 'constraint': { | ||
// 'minLength': ['minLength', '$1'], | ||
// 'format': { | ||
// 'uri': ['url', '$2'], | ||
// 'email': ['email', '$2'], | ||
// } | ||
// } | ||
// } | ||
// | ||
// GraphQL schema | ||
// ```graphql | ||
// input ExampleInput { | ||
// email: String! @required(msg: "message") @constraint(minLength: 100, format: "email") | ||
// } | ||
// ``` | ||
// | ||
// FIXME: v.required() is not supported yet. v.required() is classified as `Methods` and must wrap the schema. ex) `v.required(v.object({...}))` | ||
export function buildApiForValibot(config: FormattedDirectiveConfig, directives: ReadonlyArray<ConstDirectiveNode>): string[] { | ||
return directives | ||
.filter(directive => config[directive.name.value] !== undefined) | ||
.map((directive) => { | ||
const directiveName = directive.name.value; | ||
const argsConfig = config[directiveName]; | ||
const apis = _buildApiFromDirectiveArguments(argsConfig, directive.arguments ?? []); | ||
return apis.map(api => `v${api}`); | ||
}).flat() | ||
} | ||
|
||
function buildApiSchema(validationSchema: string[] | undefined, argValue: ConstValueNode): string { | ||
if (!validationSchema) | ||
return ''; | ||
|
@@ -133,6 +166,10 @@ function buildApiSchema(validationSchema: string[] | undefined, argValue: ConstV | |
} | ||
|
||
function buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, args: ReadonlyArray<ConstArgumentNode>): string { | ||
return _buildApiFromDirectiveArguments(config, args).join(''); | ||
} | ||
|
||
function _buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, args: ReadonlyArray<ConstArgumentNode>): string[] { | ||
return args | ||
.map((arg) => { | ||
const argName = arg.name.value; | ||
|
@@ -142,7 +179,6 @@ function buildApiFromDirectiveArguments(config: FormattedDirectiveArguments, arg | |
|
||
return buildApiSchema(validationSchema, arg.value); | ||
}) | ||
.join(''); | ||
} | ||
|
||
function buildApiFromDirectiveObjectArguments(config: FormattedDirectiveObjectArguments, argValue: ConstValueNode): string { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valibot is planning big breaking changes to its API in v.0.31.0. Since the old API is no longer available, this PR uses the rc version to comply with the new API.
ref: Migrate from v0.30.0 | Valibot