@@ -122,4 +122,91 @@ describe('SchemaValidator', () => {
122122 } ;
123123 expect ( SchemaValidator . validate ( schema , params ) ) . not . toBeNull ( ) ;
124124 } ) ;
125+
126+ describe ( 'boolean string coercion' , ( ) => {
127+ const booleanSchema = {
128+ type : 'object' ,
129+ properties : {
130+ is_background : {
131+ type : 'boolean' ,
132+ } ,
133+ } ,
134+ required : [ 'is_background' ] ,
135+ } ;
136+
137+ it ( 'should coerce string "true" to boolean true' , ( ) => {
138+ const params = { is_background : 'true' } ;
139+ expect ( SchemaValidator . validate ( booleanSchema , params ) ) . toBeNull ( ) ;
140+ expect ( params . is_background ) . toBe ( true ) ;
141+ } ) ;
142+
143+ it ( 'should coerce string "True" to boolean true' , ( ) => {
144+ const params = { is_background : 'True' } ;
145+ expect ( SchemaValidator . validate ( booleanSchema , params ) ) . toBeNull ( ) ;
146+ expect ( params . is_background ) . toBe ( true ) ;
147+ } ) ;
148+
149+ it ( 'should coerce string "TRUE" to boolean true' , ( ) => {
150+ const params = { is_background : 'TRUE' } ;
151+ expect ( SchemaValidator . validate ( booleanSchema , params ) ) . toBeNull ( ) ;
152+ expect ( params . is_background ) . toBe ( true ) ;
153+ } ) ;
154+
155+ it ( 'should coerce string "false" to boolean false' , ( ) => {
156+ const params = { is_background : 'false' } ;
157+ expect ( SchemaValidator . validate ( booleanSchema , params ) ) . toBeNull ( ) ;
158+ expect ( params . is_background ) . toBe ( false ) ;
159+ } ) ;
160+
161+ it ( 'should coerce string "False" to boolean false' , ( ) => {
162+ const params = { is_background : 'False' } ;
163+ expect ( SchemaValidator . validate ( booleanSchema , params ) ) . toBeNull ( ) ;
164+ expect ( params . is_background ) . toBe ( false ) ;
165+ } ) ;
166+
167+ it ( 'should coerce string "FALSE" to boolean false' , ( ) => {
168+ const params = { is_background : 'FALSE' } ;
169+ expect ( SchemaValidator . validate ( booleanSchema , params ) ) . toBeNull ( ) ;
170+ expect ( params . is_background ) . toBe ( false ) ;
171+ } ) ;
172+
173+ it ( 'should handle nested objects with string booleans' , ( ) => {
174+ const nestedSchema = {
175+ type : 'object' ,
176+ properties : {
177+ options : {
178+ type : 'object' ,
179+ properties : {
180+ enabled : { type : 'boolean' } ,
181+ } ,
182+ } ,
183+ } ,
184+ } ;
185+ const params = { options : { enabled : 'true' } } ;
186+ expect ( SchemaValidator . validate ( nestedSchema , params ) ) . toBeNull ( ) ;
187+ expect ( ( params . options as unknown as { enabled : boolean } ) . enabled ) . toBe (
188+ true ,
189+ ) ;
190+ } ) ;
191+
192+ it ( 'should not affect non-boolean strings' , ( ) => {
193+ const mixedSchema = {
194+ type : 'object' ,
195+ properties : {
196+ name : { type : 'string' } ,
197+ is_active : { type : 'boolean' } ,
198+ } ,
199+ } ;
200+ const params = { name : 'trueman' , is_active : 'true' } ;
201+ expect ( SchemaValidator . validate ( mixedSchema , params ) ) . toBeNull ( ) ;
202+ expect ( params . name ) . toBe ( 'trueman' ) ;
203+ expect ( params . is_active ) . toBe ( true ) ;
204+ } ) ;
205+
206+ it ( 'should pass through actual boolean values unchanged' , ( ) => {
207+ const params = { is_background : true } ;
208+ expect ( SchemaValidator . validate ( booleanSchema , params ) ) . toBeNull ( ) ;
209+ expect ( params . is_background ) . toBe ( true ) ;
210+ } ) ;
211+ } ) ;
125212} ) ;
0 commit comments