diff --git a/CHANGELOG.md b/CHANGELOG.md index a3a35a4..99720d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ #### 🚀 Enhancement -- Add regex validator [#17](https://github.com/opencreek/vlad/pull/17) ([@reckter](https://github.com/reckter)) +- Add regex validator [#17](https://github.com/opencreek/vlad/pull/17) + ([@reckter](https://github.com/reckter)) #### Authors: 1 diff --git a/polyfill/objectHasOwn.ts b/polyfill/objectHasOwn.ts index c1565ed..0e4e3c7 100644 --- a/polyfill/objectHasOwn.ts +++ b/polyfill/objectHasOwn.ts @@ -1,6 +1,5 @@ declare global { interface Object { - // deno-lint-ignore ban-types hasOwn(subject: object, property: PropertyKey): boolean; } } diff --git a/src/validators/includeIf.ts b/src/validators/includeIf.ts index 2e6b36a..fe69cf9 100644 --- a/src/validators/includeIf.ts +++ b/src/validators/includeIf.ts @@ -33,12 +33,16 @@ export type Condition = * ``` */ -export function includeIf( +export function includeIf< + // deno-lint-ignore no-explicit-any + Subject extends any, + V1 extends Validator, +>( condition: - | ((value: SubjectType, context: ContextType) => boolean) + | ((value: Subject, context: ContextType) => boolean) | boolean, validator1: V1, -): V1 { +): Validator & Subject, ReturnType> { return function includeIfValidator(subject, context) { const conditionResult = resolveCondition(subject, context, condition); diff --git a/src/validators/requiredObject.ts b/src/validators/requiredObject.ts index 8b365e5..cf35f34 100644 --- a/src/validators/requiredObject.ts +++ b/src/validators/requiredObject.ts @@ -23,7 +23,6 @@ export function requiredObject( message: string, ): Validator< // we actually want all non-primitives here - // deno-lint-ignore ban-types object, ObjectTopLevelError >; diff --git a/test/validators/includeIf.test.ts b/test/validators/includeIf.test.ts index 38b4868..de9e0a7 100644 --- a/test/validators/includeIf.test.ts +++ b/test/validators/includeIf.test.ts @@ -1,4 +1,10 @@ -import { includeIf, is, minItems } from "../../src/vlad.ts"; +import { + includeIf, + is, + minItems, + object, + requiredPrimitive, +} from "../../src/vlad.ts"; import { assertEquals } from "../testingDeps.ts"; Deno.test("should not change the result of a single is validator", () => { @@ -13,7 +19,7 @@ Deno.test("should not change the result of a single is validator", () => { Deno.test("Should correctly include validator on condition", () => { const validator = includeIf( - (value) => (value?.[0] ?? 0) < 5, + (value: Array) => (value?.[0] ?? 0) < 5, minItems(4, "Should have at least 5 items"), ); const outputInvalid = validator([1]); @@ -42,3 +48,19 @@ Deno.test("Should correctly include validator on static condition - false", () = assertEquals(outputInvalid, undefined); }); + +Deno.test("Should correctly infer types from the boolean condition", () => { + const validator = includeIf( + (value: { length: number }) => value.length > 5, + object({ + foo: requiredPrimitive("foo is required"), + }), + ); + const outputInvalid = validator({ length: 6 }); + const outputValid = validator({ length: 6, foo: "bla" }); + const outputValid2 = validator({ length: 2 }); + + assertEquals(outputInvalid, { foo: ["foo is required"] }); + assertEquals(outputValid, undefined); + assertEquals(outputValid2, undefined); +});