File tree 2 files changed +23
-1
lines changed
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ const Serializer = require('./lib/serializer')
12
12
const Validator = require ( './lib/validator' )
13
13
const Location = require ( './lib/location' )
14
14
15
+ const SINGLE_TICK = / ' / g
16
+
15
17
let largeArraySize = 2e4
16
18
let largeArrayMechanism = 'default'
17
19
@@ -834,7 +836,7 @@ function buildConstSerializer (location, input) {
834
836
`
835
837
}
836
838
837
- code += `json += '${ JSON . stringify ( schema . const ) } '`
839
+ code += `json += '${ JSON . stringify ( schema . const ) . replace ( SINGLE_TICK , "\\'" ) } '`
838
840
839
841
if ( hasNullType ) {
840
842
code += `
Original file line number Diff line number Diff line change @@ -82,6 +82,26 @@ test('schema with const string and no input', (t) => {
82
82
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
83
83
} )
84
84
85
+ test ( 'schema with const string that contains \'' , ( t ) => {
86
+ t . plan ( 2 )
87
+
88
+ const schema = {
89
+ type : 'object' ,
90
+ properties : {
91
+ foo : { const : "'bar'" }
92
+ }
93
+ }
94
+
95
+ const validate = validator ( schema )
96
+ const stringify = build ( schema )
97
+ const output = stringify ( {
98
+ foo : "'bar'"
99
+ } )
100
+
101
+ t . equal ( output , '{"foo":"\'bar\'"}' )
102
+ t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
103
+ } )
104
+
85
105
test ( 'schema with const number' , ( t ) => {
86
106
t . plan ( 2 )
87
107
You can’t perform that action at this time.
0 commit comments