import SchemaItem from '../SchemaItem' import { SchemaInfer, ValidationResult } from '../types' type ItemType> = SchemaInfer export default class SchemaUnion> extends SchemaItem> { private schemas: T public constructor(...schemas: T) { super() this.schemas = schemas } public parse(input: unknown, options?: { fast?: boolean }): ValidationResult> { // check errors from itself const { valid, object, errors = [] } = super.parse(input, options) // skip checking childs if self is not valid (maybe still try to check childs whan fast is false ?) if (!valid) { return { valid, object, errors } as ValidationResult> } // loop through the childs for (const schema of this.schemas) { const validation = schema.parse(input, options) if (validation.valid) { return validation } else { errors.push(...validation.errors) } } // answer ! return { valid: false, errors: errors, object: object } as ValidationResult> } public override isOfType(input: unknown): input is ItemType { return this.schemas.some((it) => it.isOfType(input)) } }