Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into improve-zod-test
Browse files Browse the repository at this point in the history
  • Loading branch information
MH4GF committed Jun 10, 2024
2 parents b820268 + aae68ab commit 8d16f2b
Show file tree
Hide file tree
Showing 13 changed files with 2,047 additions and 608 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v3
with:
version: 8
package_json_file: ./package.json
- name: Setup Node
uses: actions/setup-node@v3
with:
Expand All @@ -25,9 +25,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v3
with:
version: 8
package_json_file: ./package.json
- name: Setup Node
uses: actions/setup-node@v3
with:
Expand Down
23 changes: 23 additions & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,26 @@ generates:
email: email
scalars:
ID: string
example/valibot/schemas.ts:
plugins:
- ./dist/main/index.js:
schema: valibot
importFrom: ../types
withObjectType: true
directives:
# Write directives like
#
# directive:
# arg1: schemaApi
# arg2: ["schemaApi2", "Hello $1"]
#
# See more examples in `./tests/directive.spec.ts`
# https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/main/tests/directive.spec.ts
constraint:
minLength: minLength
# Replace $1 with specified `startsWith` argument value of the constraint directive
startsWith: [regex, /^$1/, message]
format:
email: email
scalars:
ID: string
5 changes: 4 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ export default antfu({
'README.md',
],
}, {
rules: { 'style/semi': 'off' },
rules: {
'style/semi': 'off',
'regexp/no-unused-capturing-group': 'off',
},
})
130 changes: 130 additions & 0 deletions example/valibot/schemas.ts
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()])
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "graphql-codegen-typescript-validation-schema",
"version": "0.14.1",
"packageManager": "pnpm@9.2.0",
"description": "GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema",
"respository": {
"type": "git",
Expand Down Expand Up @@ -46,6 +47,7 @@
"type-check:yup": "tsc --strict --skipLibCheck --noEmit example/yup/schemas.ts",
"type-check:zod": "tsc --strict --skipLibCheck --noEmit example/zod/schemas.ts",
"type-check:myzod": "tsc --strict --skipLibCheck --noEmit example/myzod/schemas.ts",
"type-check:valibot": "tsc --strict --skipLibCheck --noEmit example/valibot/schemas.ts",
"test": "vitest run",
"build": "run-p build:*",
"build:main": "tsc -p tsconfig.main.json",
Expand Down Expand Up @@ -74,13 +76,14 @@
"@tsconfig/recommended": "1.0.6",
"@types/graphlib": "^2.1.8",
"@types/node": "^20.0.0",
"eslint": "9.3.0",
"eslint": "9.4.0",
"jest": "29.7.0",
"myzod": "1.11.0",
"npm-run-all2": "6.2.0",
"ts-dedent": "^2.2.0",
"ts-jest": "29.1.3",
"ts-jest": "29.1.4",
"typescript": "5.4.5",
"valibot": "0.31.0-rc.12",
"vitest": "^1.0.0",
"yup": "1.4.0",
"zod": "3.23.8"
Expand Down
Loading

0 comments on commit 8d16f2b

Please sign in to comment.