1
+ 'use strict'
2
+
3
+ const { test } = require ( 'node:test' )
4
+
5
+ const build = require ( '..' )
6
+
7
+ test ( 'serialize string with quotes - issue #794' , ( t ) => {
8
+ t . plan ( 2 )
9
+
10
+ const schema = {
11
+ type : 'object' ,
12
+ properties : {
13
+ message : {
14
+ type : 'string'
15
+ }
16
+ }
17
+ }
18
+
19
+ const input = {
20
+ message : 'Error: Property "name" is required'
21
+ }
22
+
23
+ const stringify = build ( schema )
24
+ const output = stringify ( input )
25
+
26
+ // The output should be valid JSON
27
+ t . assert . doesNotThrow ( ( ) => {
28
+ JSON . parse ( output )
29
+ } , 'JSON output should be parseable' )
30
+
31
+ // The parsed output should match the input
32
+ const parsed = JSON . parse ( output )
33
+ t . assert . equal ( parsed . message , input . message )
34
+ } )
35
+
36
+ test ( 'serialize string with various quote types - issue #794' , ( t ) => {
37
+ t . plan ( 6 )
38
+
39
+ const schema = {
40
+ type : 'string'
41
+ }
42
+
43
+ const stringify = build ( schema )
44
+
45
+ // Test double quotes
46
+ const inputDoubleQuotes = 'Property "name" is required'
47
+ const outputDoubleQuotes = stringify ( inputDoubleQuotes )
48
+ t . assert . doesNotThrow ( ( ) => JSON . parse ( outputDoubleQuotes ) )
49
+ t . assert . equal ( JSON . parse ( outputDoubleQuotes ) , inputDoubleQuotes )
50
+
51
+ // Test single quotes (should be fine but test for completeness)
52
+ const inputSingleQuotes = "Property 'name' is required"
53
+ const outputSingleQuotes = stringify ( inputSingleQuotes )
54
+ t . assert . doesNotThrow ( ( ) => JSON . parse ( outputSingleQuotes ) )
55
+ t . assert . equal ( JSON . parse ( outputSingleQuotes ) , inputSingleQuotes )
56
+
57
+ // Test mixed quotes
58
+ const inputMixedQuotes = 'Error: "Property \'name\' is required"'
59
+ const outputMixedQuotes = stringify ( inputMixedQuotes )
60
+ t . assert . doesNotThrow ( ( ) => JSON . parse ( outputMixedQuotes ) )
61
+ t . assert . equal ( JSON . parse ( outputMixedQuotes ) , inputMixedQuotes )
62
+ } )
63
+
64
+ test ( 'serialize error-like object with quotes in message - issue #794' , ( t ) => {
65
+ t . plan ( 2 )
66
+
67
+ const schema = {
68
+ type : 'object' ,
69
+ properties : {
70
+ error : {
71
+ type : 'object' ,
72
+ properties : {
73
+ message : {
74
+ type : 'string'
75
+ } ,
76
+ code : {
77
+ type : 'string'
78
+ }
79
+ }
80
+ }
81
+ }
82
+ }
83
+
84
+ const input = {
85
+ error : {
86
+ message : 'Validation failed: Property "email" must be a valid email address' ,
87
+ code : 'VALIDATION_ERROR'
88
+ }
89
+ }
90
+
91
+ const stringify = build ( schema )
92
+ const output = stringify ( input )
93
+
94
+ // The output should be valid JSON
95
+ t . assert . doesNotThrow ( ( ) => {
96
+ JSON . parse ( output )
97
+ } , 'JSON output should be parseable' )
98
+
99
+ // The parsed output should match the input
100
+ const parsed = JSON . parse ( output )
101
+ t . assert . deepEqual ( parsed , input )
102
+ } )
103
+
104
+ test ( 'serialize validation errors array with quotes - issue #794' , ( t ) => {
105
+ t . plan ( 2 )
106
+
107
+ const schema = {
108
+ type : 'object' ,
109
+ properties : {
110
+ errors : {
111
+ type : 'array' ,
112
+ items : {
113
+ type : 'object' ,
114
+ properties : {
115
+ message : {
116
+ type : 'string'
117
+ } ,
118
+ field : {
119
+ type : 'string'
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ const input = {
128
+ errors : [
129
+ {
130
+ message : 'Property "name" is required' ,
131
+ field : 'name'
132
+ } ,
133
+ {
134
+ message : 'Property "email" must be a valid email address' ,
135
+ field : 'email'
136
+ } ,
137
+ {
138
+ message : 'Value must be between "1" and "100"' ,
139
+ field : 'age'
140
+ }
141
+ ]
142
+ }
143
+
144
+ const stringify = build ( schema )
145
+ const output = stringify ( input )
146
+
147
+ // The output should be valid JSON
148
+ t . assert . doesNotThrow ( ( ) => {
149
+ JSON . parse ( output )
150
+ } , 'JSON output should be parseable' )
151
+
152
+ // The parsed output should match the input
153
+ const parsed = JSON . parse ( output )
154
+ t . assert . deepEqual ( parsed , input )
155
+ } )
156
+
157
+ test ( 'serialize string with backslashes and quotes - issue #794' , ( t ) => {
158
+ t . plan ( 4 )
159
+
160
+ const schema = {
161
+ type : 'string'
162
+ }
163
+
164
+ const stringify = build ( schema )
165
+
166
+ // Test backslashes
167
+ const inputBackslash = 'Path: C:\\Users\\test\\file.json'
168
+ const outputBackslash = stringify ( inputBackslash )
169
+ t . assert . doesNotThrow ( ( ) => JSON . parse ( outputBackslash ) )
170
+ t . assert . equal ( JSON . parse ( outputBackslash ) , inputBackslash )
171
+
172
+ // Test combination of backslashes and quotes
173
+ const inputMixed = 'Error: Could not find file "C:\\Users\\test\\config.json"'
174
+ const outputMixed = stringify ( inputMixed )
175
+ t . assert . doesNotThrow ( ( ) => JSON . parse ( outputMixed ) )
176
+ t . assert . equal ( JSON . parse ( outputMixed ) , inputMixed )
177
+ } )
0 commit comments