File tree Expand file tree Collapse file tree 3 files changed +18
-63
lines changed Expand file tree Collapse file tree 3 files changed +18
-63
lines changed Original file line number Diff line number Diff line change 11'use strict'
22
3- const mergeAllOf = require ( 'json-schema-merge-allof' )
4-
5- const failOnConflictResolver = mergeAllOf . options . resolvers . type
6- const pickFirstResolver = mergeAllOf . options . resolvers . title
7-
8- const resolvers = {
9- format : failOnConflictResolver ,
10- nullable : failOnConflictResolver ,
11- defaultResolver : pickFirstResolver
12- }
3+ const { mergeSchemas : _mergeSchemas } = require ( 'merge-json-schemas' )
134
145function mergeSchemas ( schemas ) {
15- let mergedSchema = null
16- try {
17- mergedSchema = mergeAllOf ( { allOf : schemas } , { resolvers } )
18- } catch ( error ) {
19- const failedPath = / ^ C o u l d n o t r e s o l v e v a l u e s f o r p a t h : " ( .* ) " \. / . exec ( error . message )
20- /* istanbul ignore else */
21- if ( failedPath ) {
22- throw new Error ( `Failed to merge schemas on "${ failedPath [ 1 ] } ".` )
23- } else {
24- throw new Error ( `Failed to merge schemas: ${ error . message } ` )
25- }
26- }
27-
28- // This is needed because fjs treats `additionalProperties` as false by default
29- // which is not true for JSON Schema.
30- if ( mergedSchema . additionalProperties === undefined ) {
31- for ( const schema of schemas ) {
32- if ( schema . additionalProperties === true ) {
33- mergedSchema . additionalProperties = true
34- break
35- }
36- }
37- }
38-
39- return mergedSchema
6+ return _mergeSchemas ( schemas , { onConflict : 'skip' } )
407}
418
429module . exports = mergeSchemas
Original file line number Diff line number Diff line change 5454 "fast-uri" : " ^2.1.0" ,
5555 "rfdc" : " ^1.2.0" ,
5656 "json-schema-ref-resolver" : " ^1.0.1" ,
57- "json-schema- merge-allof " : " ^0.8.1 "
57+ "merge-json-schemas " : " fastify/merge-json-schemas#add-merge-json-schemas-function "
5858 },
5959 "standard" : {
6060 "ignore" : [
Original file line number Diff line number Diff line change @@ -606,51 +606,39 @@ test('allOf: multiple nested $ref properties', (t) => {
606606} )
607607
608608test ( 'allOf: throw Error if types mismatch ' , ( t ) => {
609- t . plan ( 1 )
609+ t . plan ( 3 )
610610
611611 const schema = {
612612 allOf : [
613613 { type : 'string' } ,
614614 { type : 'number' }
615615 ]
616616 }
617- t . throws ( ( ) => build ( schema ) , new Error ( 'Failed to merge schemas on "type".' ) )
617+ try {
618+ build ( schema )
619+ } catch ( error ) {
620+ t . ok ( error instanceof Error )
621+ t . equal ( error . message , 'Failed to merge "type" keyword schemas.' )
622+ t . same ( error . schemas , [ [ 'string' ] , [ 'number' ] ] )
623+ }
618624} )
619625
620626test ( 'allOf: throw Error if format mismatch ' , ( t ) => {
621- t . plan ( 1 )
627+ t . plan ( 3 )
622628
623629 const schema = {
624630 allOf : [
625631 { format : 'date' } ,
626632 { format : 'time' }
627633 ]
628634 }
629- t . throws ( ( ) => build ( schema ) , new Error ( 'Failed to merge schemas on "format".' ) )
630- } )
631-
632- test ( 'allOf: throw Error if nullable mismatch /1' , ( t ) => {
633- t . plan ( 1 )
634-
635- const schema = {
636- allOf : [
637- { nullable : true } ,
638- { nullable : false }
639- ]
640- }
641- t . throws ( ( ) => build ( schema ) , new Error ( 'Failed to merge schemas on "nullable".' ) )
642- } )
643-
644- test ( 'allOf: throw Error if nullable mismatch /2' , ( t ) => {
645- t . plan ( 1 )
646-
647- const schema = {
648- allOf : [
649- { nullable : false } ,
650- { nullable : true }
651- ]
635+ try {
636+ build ( schema )
637+ } catch ( error ) {
638+ t . ok ( error instanceof Error )
639+ t . equal ( error . message , 'Failed to merge "format" keyword schemas.' )
640+ t . same ( error . schemas , [ 'date' , 'time' ] )
652641 }
653- t . throws ( ( ) => build ( schema ) , new Error ( 'Failed to merge schemas on "nullable".' ) )
654642} )
655643
656644test ( 'recursive nested allOfs' , ( t ) => {
You can’t perform that action at this time.
0 commit comments