-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow unions of transformations and checks
- Loading branch information
1 parent
fe0298f
commit 4caa4cc
Showing
6 changed files
with
65 additions
and
48 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
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Validator, TypeDataKinds } from "../gen/validators"; | ||
|
||
export function getUnionMembers(validators: Validator[]): { | ||
compound: Validator[]; | ||
normal: Validator[]; | ||
object: [Validator, Validator][]; | ||
isNullable: boolean; | ||
} { | ||
const compoundTypes: Validator[] = [], | ||
normalTypes: Validator[] = [], | ||
objectTypes: [Validator, Validator][] = []; | ||
let isNullable = false; | ||
const objectKind = validators.filter(v => v.typeData.kind === TypeDataKinds.Object).length; | ||
for (const child of validators) { | ||
if (child.typeData.kind === TypeDataKinds.Undefined) { | ||
isNullable = true; | ||
continue; | ||
} else if (child.children.length && child.typeData.kind === TypeDataKinds.Object && objectKind > 1) { | ||
const idRepresent = child.getFirstLiteralChild(); | ||
if (idRepresent) { | ||
child.children.splice(child.children.indexOf(idRepresent), 1); | ||
objectTypes.push([child, idRepresent]); | ||
} else compoundTypes.push(child); | ||
} else if (child.isComplexType()) compoundTypes.push(child); | ||
else normalTypes.push(child); | ||
} | ||
return { | ||
compound: compoundTypes, | ||
normal: normalTypes, | ||
object: objectTypes, | ||
isNullable | ||
}; | ||
} |