-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: respect nullable() with oneOf (#1757)
closes: #768 #104 BREAKING CHANGE: previously `oneOf` required adding `null` explicitly to allowed values when using oneOf. Folks have found this confusing and unintuitive so I am deferring and adjusting the behavior
- Loading branch information
Showing
4 changed files
with
22 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,33 @@ | ||
import type { SchemaRefDescription } from '../schema'; | ||
import Reference from '../Reference'; | ||
|
||
export default class ReferenceSet { | ||
list: Set<unknown>; | ||
refs: Map<string, Reference>; | ||
|
||
constructor() { | ||
this.list = new Set(); | ||
this.refs = new Map(); | ||
} | ||
get size() { | ||
return this.list.size + this.refs.size; | ||
} | ||
|
||
export default class ReferenceSet extends Set<unknown | Reference> { | ||
describe() { | ||
const description = [] as Array<unknown | SchemaRefDescription>; | ||
|
||
for (const item of this.list) description.push(item); | ||
for (const [, ref] of this.refs) description.push(ref.describe()); | ||
|
||
for (const item of this.values()) { | ||
description.push(Reference.isRef(item) ? item.describe() : item); | ||
} | ||
return description; | ||
} | ||
|
||
toArray() { | ||
return Array.from(this.list).concat(Array.from(this.refs.values())); | ||
} | ||
|
||
resolveAll(resolve: (v: unknown) => unknown) { | ||
return this.toArray().reduce((acc: unknown[],e) => acc.concat(Reference.isRef(e) ? resolve(e) : e),[]); | ||
} | ||
|
||
add(value: unknown) { | ||
Reference.isRef(value) | ||
? this.refs.set(value.key, value) | ||
: this.list.add(value); | ||
} | ||
delete(value: unknown) { | ||
Reference.isRef(value) | ||
? this.refs.delete(value.key) | ||
: this.list.delete(value); | ||
resolveAll(resolve: (v: unknown | Reference) => unknown) { | ||
let result = [] as unknown[]; | ||
for (const item of this.values()) { | ||
result.push(resolve(item)); | ||
} | ||
return result; | ||
} | ||
|
||
clone() { | ||
const next = new ReferenceSet(); | ||
next.list = new Set(this.list); | ||
next.refs = new Map(this.refs); | ||
return next; | ||
return new ReferenceSet(this.values()); | ||
} | ||
|
||
merge(newItems: ReferenceSet, removeItems: ReferenceSet) { | ||
const next = this.clone(); | ||
newItems.list.forEach((value) => next.add(value)); | ||
newItems.refs.forEach((value) => next.add(value)); | ||
removeItems.list.forEach((value) => next.delete(value)); | ||
removeItems.refs.forEach((value) => next.delete(value)); | ||
|
||
newItems.forEach((value) => next.add(value)); | ||
removeItems.forEach((value) => next.delete(value)); | ||
return next; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters