Skip to content

Commit

Permalink
Merge pull request #25 from lifeomic/remove-optional-keys
Browse files Browse the repository at this point in the history
fix: remove optional keys from processed json schemas
  • Loading branch information
swain authored Jun 1, 2022
2 parents fee233d + f38d731 commit 9af80ad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/meta-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('assumptions', () => {
expect: OneSchemaDefinition;
}[] = [
{
name: 'noAdditionalPropertiesOnObjects',
name: 'noAdditionalPropertiesOnObjects adds additionalProperties: false to objects',
input: {
assumptions: {
noAdditionalPropertiesOnObjects: true,
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('assumptions', () => {
},
},
{
name: 'objectPropertiesRequiredByDefault',
name: 'objectPropertiesRequiredByDefault marks all object properties as required by default',
input: {
assumptions: {
objectPropertiesRequiredByDefault: true,
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('assumptions', () => {
properties: {
id: { type: 'string' },
message: { type: 'string' },
title: { type: 'string', optional: true },
title: { type: 'string' },
},
},
},
Expand Down
67 changes: 44 additions & 23 deletions src/meta-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,41 @@ export const validateSchema = (spec: OneSchemaDefinition) => {
}
};

/**
* Applies a set of transforms to a OneSchema.
*
* The provided `transforms` are each fully completed before the next begins.
*
* @example
* transformOneSchema(
* spec,
* transformA,
* transformB
* )
* // transformA is applied to the entire schema, then transformB
* // is applied to the entire schema.
*/
const transformOneSchema = (
spec: OneSchemaDefinition,
transform: (schema: JSONSchema4) => JSONSchema4,
...transforms: ((schema: JSONSchema4) => JSONSchema4)[]
): OneSchemaDefinition => {
const copy = deepCopy(spec);

for (const key in copy.Resources) {
copy.Resources[key] = transformJSONSchema(copy.Resources[key], transform);
}
for (const transform of transforms) {
for (const key in copy.Resources) {
copy.Resources[key] = transformJSONSchema(copy.Resources[key], transform);
}

for (const key in copy.Endpoints) {
copy.Endpoints[key].Request = transformJSONSchema(
copy.Endpoints[key].Request ?? {},
transform,
);
copy.Endpoints[key].Response = transformJSONSchema(
copy.Endpoints[key].Response,
transform,
);
for (const key in copy.Endpoints) {
copy.Endpoints[key].Request = transformJSONSchema(
copy.Endpoints[key].Request ?? {},
transform,
);
copy.Endpoints[key].Response = transformJSONSchema(
copy.Endpoints[key].Response,
transform,
);
}
}

return copy;
Expand Down Expand Up @@ -145,16 +161,21 @@ export const withAssumptions = (
}

if (assumptions.objectPropertiesRequiredByDefault) {
copy = transformOneSchema(copy, (schema) =>
schema.type === 'object' && schema.properties
? {
required: Object.keys(schema.properties).filter(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(name) => schema.properties![name].optional !== true,
),
...schema,
}
: schema,
copy = transformOneSchema(
copy,
(schema) =>
schema.type === 'object' && schema.properties
? {
required: Object.keys(schema.properties).filter(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(name) => schema.properties![name].optional !== true,
),
...schema,
}
: schema,
// Prune the `optional` keywords from the schema, since they are no longer
// needed
(schema) => deepCopy({ ...schema, optional: undefined }),
);
}

Expand Down

0 comments on commit 9af80ad

Please sign in to comment.