1
+ 'use strict'
2
+
3
+ const { test } = require ( 'node:test' )
4
+
5
+ const build = require ( '..' )
6
+
7
+ test ( 'serialize string with newlines - issue #793' , ( 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 : `This is a string
21
+ with multiple
22
+ newlines in it
23
+ Foo`
24
+ }
25
+
26
+ const stringify = build ( schema )
27
+ const output = stringify ( input )
28
+
29
+ // The output should be valid JSON
30
+ t . assert . doesNotThrow ( ( ) => {
31
+ JSON . parse ( output )
32
+ } , 'JSON output should be parseable' )
33
+
34
+ // The parsed output should match the input
35
+ const parsed = JSON . parse ( output )
36
+ t . assert . equal ( parsed . message , input . message )
37
+ } )
38
+
39
+ test ( 'serialize string with various newline characters - issue #793' , ( t ) => {
40
+ t . plan ( 4 )
41
+
42
+ const schema = {
43
+ type : 'string'
44
+ }
45
+
46
+ const stringify = build ( schema )
47
+
48
+ // Test \n (line feed)
49
+ const inputLF = 'line1\nline2'
50
+ const outputLF = stringify ( inputLF )
51
+ t . assert . equal ( JSON . parse ( outputLF ) , inputLF )
52
+
53
+ // Test \r (carriage return)
54
+ const inputCR = 'line1\rline2'
55
+ const outputCR = stringify ( inputCR )
56
+ t . assert . equal ( JSON . parse ( outputCR ) , inputCR )
57
+
58
+ // Test \r\n (CRLF)
59
+ const inputCRLF = 'line1\r\nline2'
60
+ const outputCRLF = stringify ( inputCRLF )
61
+ t . assert . equal ( JSON . parse ( outputCRLF ) , inputCRLF )
62
+
63
+ // Test mixed newlines
64
+ const inputMixed = 'line1\nline2\rline3\r\nline4'
65
+ const outputMixed = stringify ( inputMixed )
66
+ t . assert . equal ( JSON . parse ( outputMixed ) , inputMixed )
67
+ } )
68
+
69
+ test ( 'serialize object with newlines in multiple properties - issue #793' , ( t ) => {
70
+ t . plan ( 2 )
71
+
72
+ const schema = {
73
+ type : 'object' ,
74
+ properties : {
75
+ message : {
76
+ type : 'string'
77
+ } ,
78
+ description : {
79
+ type : 'string'
80
+ } ,
81
+ timestamp : {
82
+ type : 'string'
83
+ }
84
+ }
85
+ }
86
+
87
+ const input = {
88
+ message : `This is a string
89
+ with multiple
90
+ newlines in it
91
+ Foo` ,
92
+ description : 'This JSON response contains a field with newline characters' ,
93
+ timestamp : new Date ( ) . toISOString ( )
94
+ }
95
+
96
+ const stringify = build ( schema )
97
+ const output = stringify ( input )
98
+
99
+ // The output should be valid JSON
100
+ t . assert . doesNotThrow ( ( ) => {
101
+ JSON . parse ( output )
102
+ } , 'JSON output should be parseable' )
103
+
104
+ // The parsed output should match the input
105
+ const parsed = JSON . parse ( output )
106
+ t . assert . deepEqual ( parsed , input )
107
+ } )
0 commit comments