Skip to content

Commit

Permalink
add normalizer rule to transform type arrays to oneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherny committed Jun 28, 2024
1 parent 8ecf50b commit 8b63de5
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,43 @@ rules.set('Add an $id to each named enum', schema => {
schema.$id = toSafeString(justName(keyName))
})

rules.set('Make implicit unions explicit', schema => {
if (!Array.isArray(schema.type)) {
return
}

// TODO: Optimize this case too
if (schema.oneOf) {
return
}

const oneOf = schema.type.map(t => {

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (20.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (20.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (20.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (21.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (20.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (17.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (16.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (17.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (16.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (21.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (20.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (21.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (20.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (17.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (16.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (18.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (19.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (17.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (21.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (17.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (18.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (19.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (19.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (16.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (19.x, windows-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (17.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (19.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (19.x, macOS-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (21.x, ubuntu-latest)

'oneOf' is declared but its value is never read.

Check failure on line 299 in src/normalizer.ts

View workflow job for this annotation

GitHub Actions / build (21.x, ubuntu-latest)

'oneOf' is declared but its value is never read.
switch (t) {
case 'object': {
const s: AnnotatedJSONSchema = {
[IsSchema]: true,
[Parent]: schema,
[Ref]: schema[Ref],
}
move(schema, s, 'patternProperties')
move(schema, s, 'properties')
move(schema, s, 'required')
if (schema[Ref]) {
delete schema[Ref]
}
return s
}
}
})
})

function move<A extends object>(from: A, to: A, key: keyof A): void {
if (key in from) {
to[key] = from[key]
delete from[key]
}
}

export function normalize(rootSchema: AnnotatedJSONSchema, filename: string, options: Options): NormalizedJSONSchema {
rules.forEach(rule => traverse(rootSchema, (schema, key) => rule(schema, filename, options, key)))
return rootSchema as NormalizedJSONSchema
Expand Down

0 comments on commit 8b63de5

Please sign in to comment.