Skip to content

Commit

Permalink
fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinraja committed Apr 7, 2023
1 parent d5a1eeb commit 368443b
Show file tree
Hide file tree
Showing 16 changed files with 844 additions and 341 deletions.
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["dbaeumer.vscode-eslint", "dprint.dprint"]
"recommendations": ["dbaeumer.vscode-eslint", "dprint.dprint"]
}
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"editor.defaultFormatter": "dprint.dprint",
"dprint.path": "node_modules/dprint/dprint"
"editor.defaultFormatter": "dprint.dprint",
"dprint.path": "node_modules/dprint/dprint"
}
136 changes: 68 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import { zodToTs } from 'zod-to-ts'

// define your Zod schema
const UserSchema = z.object({
username: z.string(),
age: z.number(),
inventory: z.object({
name: z.string(),
itemId: z.number(),
}).array(),
username: z.string(),
age: z.number(),
inventory: z.object({
name: z.string(),
itemId: z.number(),
}).array(),
})

// pass schema and name of type/identifier
Expand Down Expand Up @@ -52,18 +52,18 @@ import { createTypeAlias, zodToTs } from 'zod-to-ts'
const identifier = 'User'
const { node } = zodToTs(UserSchema, identifier)
const typeAlias = createTypeAlias(
node,
identifier,
// optionally pass a comment
// comment: UserSchema.description
node,
identifier,
// optionally pass a comment
// comment: UserSchema.description
)
```

result:

```ts
type User = {
username: string
username: string
}
```
Expand Down Expand Up @@ -127,13 +127,13 @@ import { z } from 'zod'
import { withGetType, zodToTs } from 'zod-to-ts'

const DateSchema = withGetType(
z.instanceof(Date),
(ts) => ts.factory.createIdentifier('Date'),
z.instanceof(Date),
(ts) => ts.factory.createIdentifier('Date'),
)

const ItemSchema = z.object({
name: z.string(),
date: DateSchema,
name: z.string(),
date: DateSchema,
})

const { node } = zodToTs(ItemSchema, 'Item')
Expand All @@ -143,17 +143,17 @@ result without `withGetType` override:

```ts
type Item = {
name: string
date: any
name: string
date: any
}
```
result with override:
```ts
type Item = {
name: string
date: Date
name: string
date: Date
}
```
Expand All @@ -170,13 +170,13 @@ Lazy types default to referencing the root type (`User` in the following example
// so you must define it
import { z } from 'zod'
type User = {
username: string
friends: User[]
username: string
friends: User[]
}

const UserSchema: z.ZodSchema<User> = z.object({
username: z.string(),
friends: z.lazy(() => UserSchema).array(),
username: z.string(),
friends: z.lazy(() => UserSchema).array(),
})

const { node } = zodToTs(UserSchema, 'User')
Expand All @@ -186,34 +186,34 @@ result:

```ts
type User = {
username: string
friends: User[]
username: string
friends: User[]
}
```
But what happens when the schema looks like this?
```ts
type User = {
username: string
item: {
name: string
itemId: string
}
friends: User[]
username: string
item: {
name: string
itemId: string
}
friends: User[]
}

// essentially when you are referencing a different field
// and not the root type
const friendItems = z.lazy(() => UserSchema.item).array()

const UserSchema: z.ZodSchema<User> = z.object({
username: z.string(),
item: z.object({
name: z.string(),
id: z.number(),
}),
friendItems,
username: z.string(),
item: z.object({
name: z.string(),
id: z.number(),
}),
friendItems,
})

const { node } = zodToTs(UserSchema, 'User')
Expand All @@ -239,34 +239,34 @@ result:
import { z } from 'zod'
import { withGetType } from 'zod-to-ts'
type User = {
username: string
item: {
name: string
id: number
}
friends: User[]
username: string
item: {
name: string
id: number
}
friends: User[]
}

const friendItems: z.Schema<User['item'][]> = withGetType(
z.lazy(() => UserSchema.item).array(),
// return a TS AST node
(ts, identifier) =>
ts.factory.createIndexedAccessTypeNode(
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(identifier),
undefined,
),
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('item')),
),
z.lazy(() => UserSchema.item).array(),
// return a TS AST node
(ts, identifier) =>
ts.factory.createIndexedAccessTypeNode(
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(identifier),
undefined,
),
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('item')),
),
)

const UserSchema: z.ZodSchema<User> = z.object({
username: z.string(),
item: z.object({
name: z.string(),
id: z.number(),
}),
friendItems,
username: z.string(),
item: z.object({
name: z.string(),
id: z.number(),
}),
friendItems,
})

const { node } = zodToTs(UserSchema, 'User')
Expand Down Expand Up @@ -326,21 +326,21 @@ import { z } from 'zod'
import { withGetType, zodToTs } from 'zod-to-ts'

enum Fruit {
Apple = 'apple',
Banana = 'banana',
Cantaloupe = 'cantaloupe',
Apple = 'apple',
Banana = 'banana',
Cantaloupe = 'cantaloupe',
}

const fruitNativeEnum = withGetType(
z.nativeEnum(
Fruit,
),
// return an identifier that will be used on the enum type
(ts) => ts.factory.createIdentifier('Fruit'),
z.nativeEnum(
Fruit,
),
// return an identifier that will be used on the enum type
(ts) => ts.factory.createIdentifier('Fruit'),
)

const TreeSchema = z.object({
fruit: fruitNativeEnum,
fruit: fruitNativeEnum,
})

const { node } = zodToTs(TreeSchema)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@types/node": "18.15.11",
"dprint": "0.35.3",
"eslint": "8.37.0",
"npm-run-all": "4.1.5",
"tsup": "6.7.0",
"tsx": "3.12.6",
"typescript": "4.9.4",
Expand Down
Loading

0 comments on commit 368443b

Please sign in to comment.