Skip to content

Commit

Permalink
Merge 1dfe880 into be2fb0e
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliowaitforitdavide committed Jan 25, 2023
2 parents be2fb0e + 1dfe880 commit 0265e7b
Show file tree
Hide file tree
Showing 6 changed files with 2,782 additions and 44 deletions.
64 changes: 44 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,52 @@ const baseSchema = {
}

function mergeJsonSchemas(schema, otherSchema) {
const { properties: schemaProperties, required: requiredSchema = [], ...schemaRemainingProperties } = schema
const {
properties: otherSchemaProperties,
required: requiredOtherSchema = [],
...otherSchemaRemainingProperties
} = otherSchema
const mergedSchema = {
type: 'object',
properties: {
...schemaProperties,
...otherSchemaProperties,
},
required: [...requiredSchema, ...requiredOtherSchema],
allOf: [
schemaRemainingProperties,
otherSchemaRemainingProperties,
],
additionalProperties: false,
type: 'object',
}

Object.keys(schema).forEach(key => {
if (Array.isArray(schema[key])) {
mergedSchema[key] = [
...mergedSchema[key] ?? [],
...schema[key],
]
return true
}

if (typeof schema[key] === 'object') {
mergedSchema[key] = {
...mergedSchema[key] ?? {},
...schema[key],
}
return true
}
})

Object.keys(otherSchema).forEach(key => {
if (Array.isArray(otherSchema[key])) {
mergedSchema[key] = [
...mergedSchema[key] ?? [],
...otherSchema[key],
]
return true
}

if (typeof otherSchema[key] === 'object') {
mergedSchema[key] = {
...mergedSchema[key] ?? {},
...otherSchema[key],
}
return true
}
})

return mergedSchema
}

function getOverlappingKeys(properties, otherProperties) {
if (!otherProperties) { return [] }
const propertiesNames = Object.keys(properties)
const otherPropertiesNames = Object.keys(otherProperties)
const overlappingProperties = propertiesNames.filter(propertyName =>
Expand Down Expand Up @@ -315,16 +338,17 @@ async function decorateRequestAndFastifyInstance(fastify, { asyncInitFunction, s
})
}

const defaultSchema = { type: 'object', required: [], properties: {} }

function initCustomServiceEnvironment(envSchema = defaultSchema) {
function initCustomServiceEnvironment(envSchema) {
return function customService(asyncInitFunction, serviceOptions) {
async function index(fastify, opts) {
const overlappingPropertiesNames = getOverlappingKeys(baseSchema.properties, envSchema.properties)
const overlappingPropertiesNames = getOverlappingKeys(baseSchema.properties, envSchema?.properties)
if (overlappingPropertiesNames.length > 0) {
throw new Error(`The provided Environment JSON Schema includes properties declared in the Base JSON Schema of the custom-plugin-lib, please remove them from your schema. The properties to remove are: ${overlappingPropertiesNames.join(', ')}`)
}
fastify.register(fastifyEnv, { schema: mergeJsonSchemas(baseSchema, envSchema), data: opts })

const schema = envSchema ? mergeJsonSchemas(baseSchema, envSchema) : baseSchema
fastify.register(fastifyEnv, { schema, data: opts })
fastify.register(fastifyFormbody)
fastify.register(fp(decorateRequestAndFastifyInstance), { asyncInitFunction, serviceOptions })
}
Expand Down
Loading

0 comments on commit 0265e7b

Please sign in to comment.