Skip to content

Commit

Permalink
* Added the value's expected type to the function name (e.g. requireT…
Browse files Browse the repository at this point in the history
…hatString() instead of requireThat()). This is the only way to ensure that the value's compile-time and runtime types match.

* Previous validator implementations failed to take undefined values into consideration.
  • Loading branch information
cowwoc committed Sep 8, 2024
1 parent 62c651e commit 259e45a
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 121 deletions.
2 changes: 0 additions & 2 deletions build/Project.mts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class Project

/**
* @param sources - the files to compile
* @private
*/
private async bundleForNode(sources: string[])
{
Expand Down Expand Up @@ -410,7 +409,6 @@ class Project

/**
* @returns the resources in the project
* @private
*/
private getResourceFiles()
{
Expand Down
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export default tsEslint.config(
"@typescript-eslint/restrict-template-expressions": [
"error",
{
allowArray: true,
allowBoolean: true,
allowNullish: true,
allowNumber: true
}
]
Expand Down
29 changes: 13 additions & 16 deletions src/DefaultJavascriptValidators.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type ArrayValidator,
type SetValidator,
type MapValidator,
type ObjectValidator,
type UnknownValidator,
Configuration,
JavascriptValidatorsImpl,
MainApplicationScope,
Expand Down Expand Up @@ -153,7 +153,7 @@ function requireThatString<T extends string | undefined | null>
}

/**
* Validates the state of an object.
* Validates the state of an unknown value or a value that does not have a specialized validator.
* <p>
* The returned validator throws an exception immediately if a validation fails. This exception is then
* converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted.
Expand All @@ -165,10 +165,9 @@ function requireThatString<T extends string | undefined | null>
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` is empty
*/
function requireThatObject<T extends object | undefined | null>
(value: T, name: string): ObjectValidator<T>
function requireThat<T>(value: T, name: string): UnknownValidator<T>
{
return DELEGATE.requireThatObject<T>(value, name);
return DELEGATE.requireThat<T>(value, name);
}

/**
Expand Down Expand Up @@ -290,7 +289,7 @@ function assertThatString<T extends string | undefined | null>
}

/**
* Validates the state of a number.
* Validates the state of an unknown value or a value that does not have a specialized validator.
* <p>
* The returned validator throws an exception immediately if a validation fails. This exception is then
* converted into an {@link AssertionError}. Exceptions unrelated to validation failures are not converted.
Expand All @@ -302,10 +301,9 @@ function assertThatString<T extends string | undefined | null>
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` is empty
*/
function assertThatObject<T extends object | undefined | null>
(value: T, name?: string): ObjectValidator<T>
function assertThat<T>(value: T, name?: string): UnknownValidator<T>
{
return DELEGATE.assertThatObject<T>(value, name);
return DELEGATE.assertThat<T>(value, name);
}

/**
Expand Down Expand Up @@ -421,7 +419,7 @@ function checkIfString<T extends string | undefined | null>
}

/**
* Validates the state of an object.
* Validates the state of an unknown value or a value that does not have a specialized validator.
* <p>
* The returned validator throws an error immediately if a validation fails.
*
Expand All @@ -435,10 +433,9 @@ function checkIfString<T extends string | undefined | null>
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` is empty
*/
function checkIfObject<T extends object | undefined | null>
(value: T, name: string): ObjectValidator<T>
function checkIf<T>(value: T, name: string): UnknownValidator<T>
{
return DELEGATE.checkIfObject<T>(value, name);
return DELEGATE.checkIf<T>(value, name);
}

/**
Expand Down Expand Up @@ -521,21 +518,21 @@ export {
requireThatSet,
requireThatMap,
requireThatString,
requireThatObject,
requireThat,
assertThatNumber,
assertThatBoolean,
assertThatArray,
assertThatSet,
assertThatMap,
assertThatString,
assertThatObject,
assertThat,
checkIfNumber,
checkIfBoolean,
checkIfArray,
checkIfSet,
checkIfMap,
checkIfString,
checkIfObject,
checkIf,
updateConfiguration,
getContext,
withContext,
Expand Down
6 changes: 3 additions & 3 deletions src/JavascriptAssertThat.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type MapValidator,
type StringValidator,
type NumberValidator,
type ObjectValidator,
type UnknownValidator,
type ArrayValidator,
AssertionError
} from "./internal/internal.mjs";
Expand Down Expand Up @@ -122,7 +122,7 @@ interface JavascriptAssertThat
assertThatString<T extends string | undefined | null>(value: T, name?: string): StringValidator<T>;

/**
* Validates the state of an `object`.
* Validates the state of an unknown value or a value that does not have a specialized validator.
* <p>
* The returned validator throws an error immediately if a validation fails. This error is then
* converted into an {@link AssertionError}. Errors unrelated to validation failures are not converted.
Expand All @@ -134,7 +134,7 @@ interface JavascriptAssertThat
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` contains whitespace or is empty
*/
assertThatObject<T extends object | undefined | null>(value: T, name?: string): ObjectValidator<T>;
assertThat<T>(value: T, name?: string): UnknownValidator<T>;
}

export type {JavascriptAssertThat};
6 changes: 3 additions & 3 deletions src/JavascriptCheckIf.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type BooleanValidator,
type StringValidator,
type NumberValidator,
type ObjectValidator,
type UnknownValidator,
type ArrayValidator,
type SetValidator,
type MapValidator
Expand Down Expand Up @@ -120,7 +120,7 @@ interface JavascriptCheckIf
checkIfString<T extends string | undefined | null>(value: T, name: string): StringValidator<T>;

/**
* Validates the state of an `object`.
* Validates the state of an unknown value or a value that does not have a specialized validator.
* <p>
* The returned validator captures errors on validation failure rather than throwing them immediately.
* These errors can be retrieved or thrown once the validation completes. Errors unrelated to
Expand All @@ -133,7 +133,7 @@ interface JavascriptCheckIf
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` contains whitespace or is empty
*/
checkIfObject<T extends object | undefined | null>(value: T, name: string): ObjectValidator<T>;
checkIf<T>(value: T, name: string): UnknownValidator<T>;
}

export type {JavascriptCheckIf};
6 changes: 3 additions & 3 deletions src/JavascriptRequireThat.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type SetValidator,
type MapValidator,
type StringValidator,
type ObjectValidator
type UnknownValidator
} from "./internal/internal.mjs";

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ interface JavascriptRequireThat
requireThatString<T extends string | undefined | null>(value: T, name: string): StringValidator<T>;

/**
* Validates the state of an `object`.
* Validates the state of an unknown value or a value that does not have a specialized validator.
* <p>
* The returned validator throws an error immediately if a validation fails.
*
Expand All @@ -118,7 +118,7 @@ interface JavascriptRequireThat
* @throws TypeError if `name` is `undefined` or `null`
* @throws RangeError if `name` contains whitespace or is empty
*/
requireThatObject<T extends object | undefined | null>(value: T, name: string): ObjectValidator<T>;
requireThat<T>(value: T, name: string): UnknownValidator<T>;
}

export type {JavascriptRequireThat};
11 changes: 4 additions & 7 deletions src/JavascriptValidators.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type JavascriptCheckIf,
Configuration,
type GlobalConfiguration,
type ObjectValidator,
type UnknownValidator,
AssertionError
} from "./internal/internal.mjs";

Expand Down Expand Up @@ -83,8 +83,7 @@ abstract class JavascriptValidators
abstract requireThatString<T extends string | undefined | null>
(value: T, name: string): StringValidator<T>;

abstract requireThatObject<T extends object | undefined | null>
(value: T, name: string): ObjectValidator<T>;
abstract requireThat<T>(value: T, name: string): UnknownValidator<T>;

abstract assertThatNumber<T extends number | undefined | null>
(value: T, name?: string): NumberValidator<T>;
Expand All @@ -104,8 +103,7 @@ abstract class JavascriptValidators
abstract assertThatString<T extends string | undefined | null>
(value: T, name?: string): StringValidator<T>;

abstract assertThatObject<T extends object | undefined | null>
(value: T, name?: string): ObjectValidator<T>;
abstract assertThat<T>(value: T, name?: string): UnknownValidator<T>;

abstract checkIfNumber<T extends number | undefined | null>
(value: T, name?: string): NumberValidator<T>;
Expand All @@ -125,8 +123,7 @@ abstract class JavascriptValidators
abstract checkIfString<T extends string | undefined | null>
(value: T, name?: string): StringValidator<T>;

abstract checkIfObject<T extends object | undefined | null>
(value: T, name?: string): ObjectValidator<T>;
abstract checkIf<T>(value: T, name?: string): UnknownValidator<T>;
}

export {JavascriptValidators};
8 changes: 4 additions & 4 deletions src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ export {
requireThatSet,
requireThatMap,
requireThatString,
requireThatObject,
requireThat,
assertThatNumber,
assertThatBoolean,
assertThatArray,
assertThatSet,
assertThatMap,
assertThatString,
assertThatObject,
assertThat,
checkIfNumber,
checkIfBoolean,
checkIfArray,
checkIfSet,
checkIfMap,
checkIfString,
checkIfObject,
checkIf,
updateConfiguration,
getContext,
withContext,
Expand All @@ -35,7 +35,7 @@ export {
type MapValidator,
type NumberValidator,
type BooleanValidator,
type ObjectValidator,
type UnknownValidator,
type ElementOf,
type MapKey,
type MapValue,
Expand Down
20 changes: 10 additions & 10 deletions src/internal/internal.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// internal.js determines the library-wide loading order.
// Dependencies must be loaded before dependents.

import type {ObjectValidator} from "../validator/ObjectValidator.mjs";
import type {UnknownValidator} from "../validator/UnknownValidator.mjs";
import {
Type,
TypeCategory
Expand Down Expand Up @@ -68,7 +68,7 @@ import {AbstractApplicationScope} from "./scope/AbstractApplicationScope.mjs";
import {MainApplicationScope} from "./scope/MainApplicationScope.mjs";
import {Configuration} from "./Configuration.mjs";
import {JavascriptValidatorsImpl} from "./validator/JavascriptValidatorsImpl.mjs";
import {ObjectValidatorImpl} from "./validator/ObjectValidatorImpl.mjs";
import {UnknownValidatorImpl} from "./validator/UnknownValidatorImpl.mjs";
import type {MapValidator} from "../validator/MapValidator.mjs";
import {MapValidatorImpl} from "./validator/MapValidatorImpl.mjs";
import type {NumberValidator} from "../validator/NumberValidator.mjs";
Expand Down Expand Up @@ -101,21 +101,21 @@ import {
requireThatSet,
requireThatMap,
requireThatString,
requireThatObject,
requireThat,
assertThatNumber,
assertThatBoolean,
assertThatArray,
assertThatSet,
assertThatMap,
assertThatString,
assertThatObject,
assertThat,
checkIfNumber,
checkIfBoolean,
checkIfArray,
checkIfSet,
checkIfMap,
checkIfString,
checkIfObject,
checkIf,
updateConfiguration,
getContext,
withContext,
Expand Down Expand Up @@ -303,29 +303,29 @@ export
quoteString,
AbstractValidators,
AbstractValidator,
ObjectValidatorImpl,
UnknownValidatorImpl,
Pluralizer,
requireThatNumber,
requireThatBoolean,
requireThatArray,
requireThatSet,
requireThatMap,
requireThatString,
requireThatObject,
requireThat,
assertThatNumber,
assertThatBoolean,
assertThatArray,
assertThatSet,
assertThatMap,
assertThatString,
assertThatObject,
assertThat,
checkIfNumber,
checkIfBoolean,
checkIfArray,
checkIfSet,
checkIfMap,
checkIfString,
checkIfObject,
checkIf,
updateConfiguration,
getContext,
withContext,
Expand Down Expand Up @@ -435,7 +435,7 @@ export type
MapValidator,
NumberValidator,
BooleanValidator,
ObjectValidator,
UnknownValidator,
ElementOf,
MapKey,
MapValue,
Expand Down
2 changes: 1 addition & 1 deletion src/internal/util/AssertionError.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AssertionError extends Error
* @param message - an explanation of what went wrong
* @param options - configuration options
*/
constructor(message?: string, options?: { cause: Error })
constructor(message?: string, options?: { cause: unknown })
{
super(message, options);
this.name = this.constructor.name;
Expand Down
Loading

0 comments on commit 259e45a

Please sign in to comment.