import {isOfType} from '@myparcel/ts-utils'; import {type AnyVal, type IfAndMatcher, type IfOrMatcher, type WithCondition, type WithTarget} from '../types'; import {validateIsTruthy} from './validateIsTruthy'; import {validateIfCondition} from './validateIfCondition'; export const validateIfConditions = ( input: Partial, getValue: (target?: string) => AnyVal, ): boolean => { if (!isOfType(input, '$if')) { if (isOfType(input, '$target')) { return validateIsTruthy(input, getValue); } return true; } return input.$if.every((condition) => { let matches = true; if (isOfType(condition, '$or')) { matches = matches && condition.$or.some((condition) => validateIfCondition(condition, getValue)); } else if (isOfType(condition, '$and')) { matches = matches && condition.$and.every((condition) => validateIfCondition(condition, getValue)); } else { matches = validateIfCondition(condition, getValue); } return matches; }); };