From a49b5c231b885f41133a2c4f3bf29ccc4130fab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20G=C3=BCdelh=C3=B6fer?= Date: Tue, 17 Oct 2023 13:03:50 +0200 Subject: [PATCH 1/2] Fix some types --- CHANGELOG.md | 4 +++- src/validators/all.ts | 7 ++----- src/validators/partialReturnOrPrimitive.ts | 5 +++++ src/validators/some.ts | 20 ++++++++++---------- src/vlad.ts | 1 + test/validators/some.test.ts | 18 +++++++++++++++++- 6 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 src/validators/partialReturnOrPrimitive.ts 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..64254d5 100644 --- a/src/validators/some.ts +++ b/src/validators/some.ts @@ -21,25 +21,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"]); +}); From 5dae18e6c23096ca57fe7415b0e4beb8ebd8c278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20G=C3=BCdelh=C3=B6fer?= Date: Tue, 17 Oct 2023 13:09:23 +0200 Subject: [PATCH 2/2] Add missing import --- src/validators/some.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/validators/some.ts b/src/validators/some.ts index 64254d5..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