1
1
'use strict'
2
2
3
3
var Ajv = require ( 'ajv' )
4
+ var merge = require ( 'deepmerge' )
4
5
5
6
var uglify = null
6
7
var isLong
@@ -86,7 +87,7 @@ function build (schema, options) {
86
87
87
88
var dependencies = [ ]
88
89
var dependenciesName = [ ]
89
- if ( hasAnyOf ( schema ) || hasArrayOfTypes ( schema ) ) {
90
+ if ( hasAnyOf ( schema ) || hasArrayOfTypes ( schema ) || hasIf ( schema ) ) {
90
91
dependencies . push ( new Ajv ( ) )
91
92
dependenciesName . push ( 'ajv' )
92
93
}
@@ -143,6 +144,11 @@ function hasArrayOfTypes (schema) {
143
144
return false
144
145
}
145
146
147
+ function hasIf ( schema ) {
148
+ const str = JSON . stringify ( schema )
149
+ return / " i f " : { / . test ( str ) && / " t h e n " : { / . test ( str )
150
+ }
151
+
146
152
function $asNull ( ) {
147
153
return 'null'
148
154
}
@@ -475,21 +481,15 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
475
481
return { code : code , laterCode : laterCode }
476
482
}
477
483
478
- function buildObject ( schema , code , name , externalSchema , fullSchema ) {
479
- code += `
480
- function ${ name } (obj) {
481
- var json = '{'
482
- var addComma = false
483
- `
484
-
484
+ function buildInnerObject ( schema , name , externalSchema , fullSchema ) {
485
+ var laterCode = ''
486
+ var code = ''
485
487
if ( schema . patternProperties ) {
486
488
code += addPatternProperties ( schema , externalSchema , fullSchema )
487
489
} else if ( schema . additionalProperties && ! schema . patternProperties ) {
488
490
code += addAdditionalProperties ( schema , externalSchema , fullSchema )
489
491
}
490
492
491
- var laterCode = ''
492
-
493
493
if ( schema . allOf ) {
494
494
schema . allOf . forEach ( ( ss ) => {
495
495
var builtCode = buildCode ( ss , code , laterCode , name , externalSchema , fullSchema )
@@ -504,6 +504,60 @@ function buildObject (schema, code, name, externalSchema, fullSchema) {
504
504
laterCode = builtCode . laterCode
505
505
}
506
506
507
+ return { code : code , laterCode : laterCode }
508
+ }
509
+
510
+ function buildObject ( schema , code , name , externalSchema , fullSchema ) {
511
+ code += `
512
+ function ${ name } (obj) {
513
+ var json = '{'
514
+ var addComma = false
515
+ `
516
+
517
+ var laterCode = ''
518
+ var r , merged
519
+ if ( schema . if && schema . then ) {
520
+ merged = merge ( schema , schema . then )
521
+ delete merged . if
522
+ delete merged . then
523
+ delete merged . else
524
+
525
+ code += `
526
+ var valid = ajv.validate(${ require ( 'util' ) . inspect ( schema . if , { depth : null } ) } , obj)
527
+ if (valid) {
528
+ `
529
+
530
+ r = buildInnerObject ( merged , name , externalSchema , fullSchema )
531
+ code += r . code
532
+ laterCode = r . laterCode
533
+
534
+ code += `
535
+ }
536
+ `
537
+ if ( schema . else ) {
538
+ merged = merge ( schema , schema . else )
539
+ delete merged . if
540
+ delete merged . then
541
+ delete merged . else
542
+
543
+ code += `
544
+ else {
545
+ `
546
+
547
+ r = buildInnerObject ( merged , name , externalSchema , fullSchema )
548
+ code += r . code
549
+ laterCode = r . laterCode
550
+
551
+ code += `
552
+ }
553
+ `
554
+ }
555
+ } else {
556
+ r = buildInnerObject ( schema , name , externalSchema , fullSchema )
557
+ code += r . code
558
+ laterCode = r . laterCode
559
+ }
560
+
507
561
// Removes the comma if is the last element of the string (in case there are not properties)
508
562
code += `
509
563
json += '}'
0 commit comments