mokkabonna / json-schema-merge-allof

Simplify your schema by combining allOf
97 stars 23 forks source link

Allow Different Resolver Functionality based on Parent Field #1

Closed bspradling closed 6 years ago

bspradling commented 6 years ago

First of all, I would like to thank you for a very good allOf resolver. I really appreciate the work.

I do have a slight tricky use case that I would like to be able to solve. I am using this library to merge a lot of different schemas and at times it would be very beneficial to have a different resolver based on what the actual field is. Here would be an example:

Schema A:
{
    "country": {
        enum: [ 'US'] 
    },
    "identificationType": {
        enum: [ 'SOCIAL_SECURITY' ]
    }
}
Schema B:
{
    "country": {
        enum: [ 'CA'] 
    },
    "identificationType": {
        enum: [ 'PASSPORT' ]
    }
}

Say in this case, I would like the country field to overwrite by taking the last value, but would like the identificationType to merge all values.

Today, as far as I'm aware, I have only be able to define a function per enum. I thought I was going to be able to use the key argument but that for to support the same function across types.

I feel like this is a good case to use anyOf. Would it be possible to update to be able to resolve anyOf with merged schemas?

Thanks!

mokkabonna commented 6 years ago

Yeah I had this planned already, haven't added it to the roadmap yet. My plan was to give you the full path.

mokkabonna commented 6 years ago

In the latest release I have changed from passing a key to the resolver to passing an array of strings. You should be able to implement any type of custom logic based on the contents of that array.

So for your example above the array passed to the enum resolver would have been

['properties', 'country', 'enum']

and:

['properties', 'identificationType', 'enum']

Hope that helps :)

bspradling commented 6 years ago

Sorry I didn't comment earlier. I was able to solve my use case with this. Thank you so much for your work.

mokkabonna commented 6 years ago

Good :)