Skip to content

Commit 62b7738

Browse files
committed
added test case for escaped default value
1 parent 6b4d4fa commit 62b7738

File tree

6 files changed

+12
-3
lines changed

6 files changed

+12
-3
lines changed

src/myzod/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { Visitor } from '../visitor';
2222
import {
2323
InterfaceTypeDefinitionBuilder,
2424
ObjectTypeDefinitionBuilder,
25+
escapeGraphQLCharacters,
2526
isInput,
2627
isListType,
2728
isNamedType,
@@ -282,7 +283,7 @@ function generateFieldTypeMyZodSchema(config: ValidationSchemaPluginConfig, visi
282283
appliedDirectivesGen = `${appliedDirectivesGen}.default(${defaultValue.value})`;
283284

284285
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM)
285-
appliedDirectivesGen = `${appliedDirectivesGen}.default("${defaultValue.value}")`;
286+
appliedDirectivesGen = `${appliedDirectivesGen}.default("${escapeGraphQLCharacters(defaultValue.value)}")`;
286287
}
287288

288289
if (isNonNullType(parentType)) {

src/yup/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { Visitor } from '../visitor';
2222
import {
2323
InterfaceTypeDefinitionBuilder,
2424
ObjectTypeDefinitionBuilder,
25+
escapeGraphQLCharacters,
2526
isInput,
2627
isListType,
2728
isNamedType,
@@ -284,7 +285,7 @@ function shapeFields(fields: readonly (FieldDefinitionNode | InputValueDefinitio
284285
}
285286

286287
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM)
287-
fieldSchema = `${fieldSchema}.default("${defaultValue.value}")`;
288+
fieldSchema = `${fieldSchema}.default("${escapeGraphQLCharacters(defaultValue.value)}")`;
288289
}
289290

290291
if (isNonNullType(field.type))

src/zod/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { Visitor } from '../visitor';
2222
import {
2323
InterfaceTypeDefinitionBuilder,
2424
ObjectTypeDefinitionBuilder,
25+
escapeGraphQLCharacters,
2526
isInput,
2627
isListType,
2728
isNamedType,
@@ -295,7 +296,7 @@ function generateFieldTypeZodSchema(config: ValidationSchemaPluginConfig, visito
295296
appliedDirectivesGen = `${appliedDirectivesGen}.default(${defaultValue.value})`;
296297

297298
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM)
298-
appliedDirectivesGen = `${appliedDirectivesGen}.default("${defaultValue.value}")`;
299+
appliedDirectivesGen = `${appliedDirectivesGen}.default("${escapeGraphQLCharacters(defaultValue.value)}")`;
299300
}
300301

301302
if (isNonNullType(parentType)) {

tests/myzod.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,7 @@ describe('myzod', () => {
13761376
input PageInput {
13771377
pageType: PageType! = PUBLIC
13781378
greeting: String = "Hello"
1379+
newline: String = "Hello\\nWorld"
13791380
score: Int = 100
13801381
ratio: Float = 0.5
13811382
isMember: Boolean = true
@@ -1399,6 +1400,7 @@ describe('myzod', () => {
13991400
return myzod.object({
14001401
pageType: PageTypeSchema.default("PUBLIC"),
14011402
greeting: myzod.string().default("Hello").optional().nullable(),
1403+
newline: myzod.string().default("Hello\\nWorld").optional().nullable(),
14021404
score: myzod.number().default(100).optional().nullable(),
14031405
ratio: myzod.number().default(0.5).optional().nullable(),
14041406
isMember: myzod.boolean().default(true).optional().nullable()

tests/yup.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,7 @@ describe('yup', () => {
14001400
input PageInput {
14011401
pageType: PageType! = PUBLIC
14021402
greeting: String = "Hello"
1403+
newline: String = "Hello\\nWorld"
14031404
score: Int = 100
14041405
ratio: Float = 0.5
14051406
isMember: Boolean = true
@@ -1423,6 +1424,7 @@ describe('yup', () => {
14231424
return yup.object({
14241425
pageType: PageTypeSchema.nonNullable().default("PUBLIC"),
14251426
greeting: yup.string().defined().nullable().default("Hello").optional(),
1427+
newline: yup.string().defined().nullable().default("Hello\\nWorld").optional(),
14261428
score: yup.number().defined().nullable().default(100).optional(),
14271429
ratio: yup.number().defined().nullable().default(0.5).optional(),
14281430
isMember: yup.boolean().defined().nullable().default(true).optional()

tests/zod.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ describe('zod', () => {
539539
input PageInput {
540540
pageType: PageType! = PUBLIC
541541
greeting: String = "Hello"
542+
newline: String = "Hello\\nWorld"
542543
score: Int = 100
543544
ratio: Float = 0.5
544545
isMember: Boolean = true
@@ -562,6 +563,7 @@ describe('zod', () => {
562563
return z.object({
563564
pageType: PageTypeSchema.default("PUBLIC"),
564565
greeting: z.string().default("Hello").nullish(),
566+
newline: z.string().default("Hello\\nWorld").nullish(),
565567
score: z.number().default(100).nullish(),
566568
ratio: z.number().default(0.5).nullish(),
567569
isMember: z.boolean().default(true).nullish()

0 commit comments

Comments
 (0)