
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@anatine/graphql-codegen-zod
Advanced tools
Its a library to parse from Graphql Schema to a Zod Schema
The reason for this project is to maintain a single source of truth, between graphql and zod. This is useful for validation on forms. Inspired by codegen-graphql-yup
z.string().email()
If you only want to validate the required fields, what you can do is use the plugin in the following way
generates:
schemas.ts:
plugins:
- @anatine/graphql-codegen-zod
If you need more validations than only required fields, you have to follow this steps.
this is because in graphql instrospection we dont have access to directives
directive @validation(
pattern: String
min: Int
max: Int
requiredMessage: String
typeOf: String
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
result.graphql
directive @validation(
pattern: String
min: Int
max: Int
requiredMessage: String
typeOf: String
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
enum TestEnum {
ENUM1
ENUM2
}
type TestSampleType {
id: ID!
name: String!
email: String
}
input TestInput {
string: String
stringRequired: String!
enum: TestEnum
enumRequired: TestEnum!
scalar: EmailAddress
scalarRequired: EmailAddress!
enumArray: [TestEnum]
enumArrayRequired: [TestEnum]!
emailArray: [EmailAddress]
emailArrayRequired: [EmailAddress]!
emailRequiredArray: [EmailAddress!]
emailRequiredArrayRequired: [EmailAddress!]!
stringArray: [String]
stringArrayRequired: [String]!
stringRequiredArray: [String!]
stringRequiredArrayRequired: [String!]!
}
input RegisterAddressInput {
postalCode: TestInput!
state: [String]!
city: String!
someNumber: Int @validation(min: 10, max: 20)
someNumberFloat: Float @validation(min: 10.50, max: 20.50)
someBoolean: Boolean
ipAddress: IPAddress
line2: String @validation(min: 10, max: 20)
}
scalar EmailAddress
scalar IPAddress
codegen.yml
schema: './apps/codegen-e2e/src/*.graphql'
generates:
'./apps/codegen-e2e/generated/output.ts':
plugins:
- @anatine/graphql-codegen-zod
config:
allowEnumStringTypes: true
onlyWithValidation: false
lazy: true
zodSchemasMap:
EmailAddress: z.string().email()
IPAddress: z.string()
DateTime: z.string()
JSON: z.object()
Date: z.string()
zodTypesMap:
EmailAddress: string
FAQs
Its a library to parse from Graphql Schema to a Zod Schema
The npm package @anatine/graphql-codegen-zod receives a total of 170 weekly downloads. As such, @anatine/graphql-codegen-zod popularity was classified as not popular.
We found that @anatine/graphql-codegen-zod demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.