diff --git a/CHANGELOG.md b/CHANGELOG.md index 1771073..d35c721 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ #### 🚀 Enhancement -- Make includeIf infer types from the condition as well [#18](https://github.com/opencreek/vlad/pull/18) ([@reckter](https://github.com/reckter)) +- Make includeIf infer types from the condition as well + [#18](https://github.com/opencreek/vlad/pull/18) + ([@reckter](https://github.com/reckter)) #### Authors: 1 diff --git a/src/validators/all.ts b/src/validators/all.ts index fd985c3..91c0721 100644 --- a/src/validators/all.ts +++ b/src/validators/all.ts @@ -1,11 +1,8 @@ // @ts-nocheck Deepmerge magic, will be replaced -import { PrimitiveErrors, SubjectType, Validator } from "../types.ts"; +import { SubjectType, Validator } from "../types.ts"; import { deepMerge } from "../deps.ts"; - -type PartialReturnOrPrimitive = ReturnType extends - PrimitiveErrors | undefined ? NonNullable> - : Partial>; +import { PartialReturnOrPrimitive } from "./partialReturnOrPrimitive.ts"; /** * Builds a validator function that applies all given validators to a value, merging their results diff --git a/src/validators/partialReturnOrPrimitive.ts b/src/validators/partialReturnOrPrimitive.ts new file mode 100644 index 0000000..f92098a --- /dev/null +++ b/src/validators/partialReturnOrPrimitive.ts @@ -0,0 +1,5 @@ +import { PrimitiveErrors, Validator } from "../types.ts"; + +export type PartialReturnOrPrimitive = + ReturnType extends PrimitiveErrors | undefined ? NonNullable> + : Partial>; diff --git a/src/validators/some.ts b/src/validators/some.ts index 8182400..cd190b1 100644 --- a/src/validators/some.ts +++ b/src/validators/some.ts @@ -2,6 +2,7 @@ import { SubjectType, Validator } from "../types.ts"; import { deepMerge } from "../deps.ts"; +import { PartialReturnOrPrimitive } from "./partialReturnOrPrimitive.ts"; /** * Builds a validator function that validates, if at least one of the given validators passes @@ -21,25 +22,25 @@ import { deepMerge } from "../deps.ts"; * ``` */ // deno-fmt-ignore -export function some(validator1: V1): Validator>, Partial>> +export function some(validator1: V1): Validator>, PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2): Validator> & Partial>, Partial> & Partial>> +export function some(validator1: V1, validator2: V2): Validator> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2, validator3: V3): Validator> & Partial> & Partial>, Partial> & Partial> & Partial>> +export function some(validator1: V1, validator2: V2, validator3: V3): Validator> & Partial> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4): Validator> & Partial> & Partial> & Partial>, Partial> & Partial> & Partial> & Partial>> +export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4): Validator> & Partial> & Partial> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5): Validator> & Partial> & Partial> & Partial> & Partial>, Partial> & Partial> & Partial> & Partial> & Partial>> +export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5): Validator> & Partial> & Partial> & Partial> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6): Validator> & Partial> & Partial> & Partial> & Partial> & Partial>, Partial> & Partial> & Partial> & Partial> & Partial> & Partial>> +export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6): Validator> & Partial> & Partial> & Partial> & Partial> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6, validator7: V7): Validator> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>, Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>> +export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6, validator7: V7): Validator> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6, validator7: V7, validator8: V8): Validator> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>, Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>> +export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6, validator7: V7, validator8: V8): Validator> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6, validator7: V7, validator8: V8, validator9: V9): Validator> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>, Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>> +export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6, validator7: V7, validator8: V8, validator9: V9): Validator> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive> // deno-fmt-ignore -export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6, validator7: V7, validator8: V8, validator9: V9, validator10: V10): Validator> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>, Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>> +export function some(validator1: V1, validator2: V2, validator3: V3, validator4: V4, validator5: V5, validator6: V6, validator7: V7, validator8: V8, validator9: V9, validator10: V10): Validator> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial> & Partial>, PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive & PartialReturnOrPrimitive> export function some(...validators) { if (validators.length === 0) { diff --git a/src/vlad.ts b/src/vlad.ts index ebb077c..1cc0d4d 100644 --- a/src/vlad.ts +++ b/src/vlad.ts @@ -17,3 +17,4 @@ export * from "./validators/includeIf.ts"; export * from "./validators/regex.ts"; export * from "./types.ts"; +export { type PartialReturnOrPrimitive } from "./validators/partialReturnOrPrimitive.ts"; diff --git a/test/validators/some.test.ts b/test/validators/some.test.ts index 079e81a..fe9949d 100644 --- a/test/validators/some.test.ts +++ b/test/validators/some.test.ts @@ -1,5 +1,11 @@ import { some } from "../../src/validators/some.ts"; -import { is, maxItems, minItems } from "../../src/vlad.ts"; +import { + is, + maxItems, + minItems, + PrimitiveErrors, + Validator, +} from "../../src/vlad.ts"; import { assertEquals } from "../testingDeps.ts"; Deno.test("should not change the result of a single is validator", () => { @@ -52,3 +58,13 @@ Deno.test("should correctly merge primitive errors", () => { _self: ["Must have at least 3 items", "Must have at least 2 items"], }); }); + +Deno.test("should correctly infer types with primitive validators", () => { + const validator = some( + is(3, "must be 3"), + is(2, "must be 2"), + ) satisfies Validator, PrimitiveErrors>; + const output = validator(1); + + assertEquals(output, ["must be 3", "must be 2"]); +});