*/
isFalse(): BooleanValidator;
- getActual(): boolean | void;
+ getActual(): boolean | undefined;
}
export {type BooleanValidator};
\ No newline at end of file
diff --git a/src/BooleanVerifier.mts b/src/BooleanVerifier.mts
index d735a50..ca7fa83 100644
--- a/src/BooleanVerifier.mts
+++ b/src/BooleanVerifier.mts
@@ -5,7 +5,7 @@ import type {ExtensibleObjectVerifier} from "./internal/internal.mjs";
*
* All methods (except those found in {@link ObjectVerifier}) imply {@link isNotNull}.
*/
-interface BooleanVerifier extends ExtensibleObjectVerifier
+interface BooleanVerifier extends ExtensibleObjectVerifier
{
/**
* Ensures that the actual value is true.
diff --git a/src/ClassValidator.mts b/src/ClassValidator.mts
index 1e525a4..c147b06 100644
--- a/src/ClassValidator.mts
+++ b/src/ClassValidator.mts
@@ -10,7 +10,8 @@ import type {ExtensibleObjectValidator} from "./internal/internal.mjs";
*
* All methods (except those found in {@link ObjectValidator}) imply {@link isNotNull}.
*/
-interface ClassValidator extends ExtensibleObjectValidator
+// eslint-disable-next-line @typescript-eslint/ban-types
+interface ClassValidator extends ExtensibleObjectValidator
{
/**
* Ensures that the actual value is the specified type, or a subtype.
@@ -31,7 +32,7 @@ interface ClassValidator extends ExtensibleObjectValidator
isSubtypeOf(type: Function): ClassValidator;
// eslint-disable-next-line @typescript-eslint/ban-types
- getActual(): Function | void;
+ getActual(): Function | undefined;
}
export {type ClassValidator};
\ No newline at end of file
diff --git a/src/ClassVerifier.mts b/src/ClassVerifier.mts
index 6896aa5..821a141 100644
--- a/src/ClassVerifier.mts
+++ b/src/ClassVerifier.mts
@@ -5,7 +5,8 @@ import type {ObjectVerifier} from "./internal/internal.mjs";
*
* All methods (except those found in {@link ObjectVerifier}) imply {@link isNotNull}.
*/
-interface ClassVerifier extends ObjectVerifier
+// eslint-disable-next-line @typescript-eslint/ban-types
+interface ClassVerifier extends ObjectVerifier
{
/**
* Ensures that the actual value is the specified type, or a super-type.
diff --git a/src/DefaultRequirements.mts b/src/DefaultRequirements.mts
index c44a36f..4c817c3 100644
--- a/src/DefaultRequirements.mts
+++ b/src/DefaultRequirements.mts
@@ -1,46 +1,120 @@
import type {
GlobalConfiguration,
ObjectValidator,
- ObjectVerifier
+ ObjectVerifier,
+ StringValidator,
+ NumberValidator,
+ ClassValidator,
+ ArrayValidator,
+ SetValidator,
+ MapValidator,
+ StringVerifier,
+ NumberVerifier,
+ ClassVerifier,
+ ArrayVerifier,
+ SetVerifier,
+ MapVerifier
+} from "./internal/internal.mjs";
+import {
+ Requirements,
+ Objects,
+ ArrayValidatorImpl,
+ Pluralizer,
+ SetValidatorImpl,
+ ClassValidatorImpl,
+ ObjectValidatorImpl,
+ Configuration,
+ MainGlobalConfiguration,
+ ArrayVerifierImpl,
+ SetVerifierImpl,
+ MapVerifierImpl,
+ MapValidatorImpl,
+ ClassVerifierImpl,
+ ObjectVerifierImpl
} from "./internal/internal.mjs";
-import {Requirements} from "./internal/internal.mjs";
const typedocWorkaround: null | GlobalConfiguration = null;
// noinspection PointlessBooleanExpressionJS
if (typedocWorkaround !== null)
console.log("WORKAROUND: https://github.com/microsoft/tsdoc/issues/348");
-const delegate = new Requirements();
-
/**
* Verifies the requirements of an object.
*
+ * @typeParam T - the type the actual value
* @param actual - the actual value
* @param name - the name of the value
* @returns a verifier
* @throws TypeError if name
is null
* @throws RangeError if name
is empty
*/
-function requireThat(actual: unknown, name: string): ObjectVerifier
+function requireThat(actual: string, name: string): StringVerifier;
+function requireThat(actual: number, name: string): NumberVerifier;
+function requireThat(actual: null, name: string): ObjectVerifier;
+// eslint-disable-next-line @typescript-eslint/ban-types
+function requireThat(actual: Function, name: string): ClassVerifier;
+function requireThat(actual: Array, name: string): ArrayVerifier;
+function requireThat(actual: Set, name: string): SetVerifier;
+function requireThat(actual: Map, name: string): MapVerifier;
+function requireThat(actual: T, name: string): ObjectVerifier;
+// eslint-disable-next-line @typescript-eslint/ban-types
+function requireThat | Set | Map, E, K, V>
+(actual: T, name: string): StringVerifier | NumberVerifier | ClassVerifier | ArrayVerifier | SetVerifier | MapVerifier | ObjectVerifier
{
- return delegate.requireThat(actual, name);
+ Objects.verifyName(name, "name");
+ const config = new Configuration(MainGlobalConfiguration.INSTANCE);
+ const typeOfActual = Objects.getTypeInfo(actual);
+ if (typeOfActual.type === "array")
+ return new ArrayVerifierImpl(new ArrayValidatorImpl(config, actual as E[], name, Pluralizer.ELEMENT, []));
+ if (typeOfActual.type === "object" && typeOfActual.name === "Set")
+ return new SetVerifierImpl(new SetValidatorImpl(config, actual as Set, name, []));
+ if (typeOfActual.type === "object" && typeOfActual.name === "Map")
+ return new MapVerifierImpl(new MapValidatorImpl(config, actual as Map, name, []));
+ if (typeOfActual.type === "class")
+ {
+ // eslint-disable-next-line @typescript-eslint/ban-types
+ return new ClassVerifierImpl(new ClassValidatorImpl(config, actual as Function, name, []));
+ }
+ return new ObjectVerifierImpl(new ObjectValidatorImpl(config, actual, name, []));
}
/**
- * Verifies requirements only if assertions are enabled.
- *
- * By default, assertions are disabled.
- * See {@link GlobalConfiguration.assertionsAreEnabled | GlobalConfiguration.assertionsAreEnabled} to change
- * the default.
+ * Validates the requirements of an object.
*
- * @param requirements - the requirements to verify
+ * @typeParam T - the type the actual value
+ * @param actual - the actual value
+ * @param name - the name of the value
+ * @returns a validator
* @throws TypeError if name
is null
* @throws RangeError if name
is empty
* @see {@link GlobalConfiguration.assertionsAreEnabled | GlobalConfiguration.assertionsAreEnabled}
*/
-function assertThat(requirements: (requirements: Requirements) => void)
+function validateThat(actual: string, name: string): StringValidator;
+function validateThat(actual: number, name: string): NumberValidator;
+function validateThat(actual: null, name: string): ObjectValidator;
+// eslint-disable-next-line @typescript-eslint/ban-types
+function validateThat(actual: Function, name: string): ClassValidator;
+function validateThat(actual: Array, name: string): ArrayValidator;
+function validateThat(actual: Set, name: string): SetValidator;
+function validateThat(actual: Map, name: string): MapValidator;
+function validateThat(actual: T, name: string): ObjectValidator;
+// eslint-disable-next-line @typescript-eslint/ban-types
+function validateThat | Set | Map, E, K, V>
+(actual: T, name: string): StringValidator | NumberValidator | ClassValidator | ArrayValidator | SetValidator | MapValidator | ObjectValidator
{
- delegate.assertThat(requirements);
+ Objects.verifyName(name, "name");
+ const config = new Configuration(MainGlobalConfiguration.INSTANCE);
+ const typeOfActual = Objects.getTypeInfo(actual);
+ if (typeOfActual.type === "array")
+ return new ArrayValidatorImpl(config, actual as E[], name, Pluralizer.ELEMENT, []);
+ if (typeOfActual.type === "object" && typeOfActual.name === "Set")
+ return new SetValidatorImpl(config, actual as Set, name, []);
+ if (typeOfActual.type === "class")
+ {
+ // eslint-disable-next-line @typescript-eslint/ban-types
+ return new ClassValidatorImpl(config, actual as Function, name, []);
+ }
+ return new ObjectValidatorImpl(config, actual, name, []);
}
/**
@@ -51,35 +125,44 @@ function assertThat(requirements: (requirements: Requirements) => void)
* the default.
*
* @param requirements - the requirements to verify
- * @returns the value returned by requirements
* @throws TypeError if name
is null
* @throws RangeError if name
is empty
* @see {@link GlobalConfiguration.assertionsAreEnabled | GlobalConfiguration.assertionsAreEnabled}
*/
-function assertThatAndReturn(requirements: (requirements: Requirements) => void)
+function assertThat(requirements: (requirements: Requirements) => void)
{
- return delegate.assertThatAndReturn(requirements);
+ Objects.requireThatValueIsDefinedAndNotNull(requirements, "requirements");
+ const config = new Configuration(MainGlobalConfiguration.INSTANCE);
+ if (config.assertionsAreEnabled())
+ requirements(new Requirements());
}
/**
- * Validates the requirements of an object.
+ * Verifies requirements only if assertions are enabled.
*
- * @param actual - the actual value
- * @param name - the name of the value
- * @returns a validator
+ * By default, assertions are disabled.
+ * See {@link GlobalConfiguration.assertionsAreEnabled | GlobalConfiguration.assertionsAreEnabled} to change
+ * the default.
+ *
+ * @param requirements - the requirements to verify
+ * @returns the value returned by requirements
* @throws TypeError if name
is null
* @throws RangeError if name
is empty
* @see {@link GlobalConfiguration.assertionsAreEnabled | GlobalConfiguration.assertionsAreEnabled}
*/
-function validateThat(actual: unknown, name: string): ObjectValidator
+function assertThatAndReturn(requirements: (requirements: Requirements) => void)
{
- return delegate.validateThat(actual, name);
+ Objects.requireThatValueIsDefinedAndNotNull(requirements, "requirements");
+ const config = new Configuration(MainGlobalConfiguration.INSTANCE);
+ if (config.assertionsAreEnabled())
+ return requirements(new Requirements());
+ return undefined;
}
export
{
requireThat,
+ validateThat,
assertThat,
- assertThatAndReturn,
- validateThat
+ assertThatAndReturn
};
\ No newline at end of file
diff --git a/src/InetAddressValidator.mts b/src/InetAddressValidator.mts
index ffd1bdc..c703cc6 100644
--- a/src/InetAddressValidator.mts
+++ b/src/InetAddressValidator.mts
@@ -10,7 +10,7 @@ import type {ExtensibleObjectValidator} from "./internal/internal.mjs";
*
* All methods (except those found in {@link ObjectValidator}) imply {@link isNotNull}.
*/
-interface InetAddressValidator extends ExtensibleObjectValidator
+interface InetAddressValidator extends ExtensibleObjectValidator
{
/**
* Ensures that the actual value is an IP v4 address.
@@ -34,7 +34,7 @@ interface InetAddressValidator extends ExtensibleObjectValidator
* All methods (except those found in {@link ObjectVerifier}) imply {@link isNotNull}.
*/
-interface InetAddressVerifier extends ObjectVerifier
+interface InetAddressVerifier extends ObjectVerifier
{
/**
* Ensures that the actual value is an IP v4 address.
diff --git a/src/MapValidator.mts b/src/MapValidator.mts
index fe02b26..d89339f 100644
--- a/src/MapValidator.mts
+++ b/src/MapValidator.mts
@@ -13,52 +13,55 @@ import type {
* exceptions.
*
* All methods (except those found in {@link ObjectValidator}) imply {@link isNotNull}.
+ *
+ * @typeParam K - the type the map's keys
+ * @typeParam V - the type the map's values
*/
-interface MapValidator extends ExtensibleObjectValidator
+interface MapValidator extends ExtensibleObjectValidator, Map>
{
/**
* Ensures that value does not contain any entries
*
* @returns the updated validator
*/
- isEmpty(): MapValidator;
+ isEmpty(): MapValidator;
/**
* Ensures that value contains at least one entry.
*
* @returns the updated validator
*/
- isNotEmpty(): MapValidator;
+ isNotEmpty(): MapValidator;
/**
* @returns a validator for the Map's keys
*/
- keys(): ArrayValidator;
+ keys(): ArrayValidator;
/**
* @param consumer - a function that accepts an {@link ArrayValidator} for the Map's keys
* @returns the updated validator
* @throws TypeError if consumer
is not set
*/
- keysConsumer(consumer: (actual: ArrayValidator) => void): MapValidator;
+ keysConsumer(consumer: (actual: ArrayValidator) => void): MapValidator;
/**
* @returns a validator for the Map's values
*/
- values(): ArrayValidator;
+ values(): ArrayValidator;
/**
* @param consumer - a function that accepts an {@link ArrayValidator} for the Map's values
* @returns the updated validator
* @throws TypeError if consumer
is not set
*/
- valuesConsumer(consumer: (actual: ArrayValidator) => void): MapValidator;
+ valuesConsumer(consumer: (actual: ArrayValidator) => void): MapValidator;
/**
* @returns validator for the Map's entries (an array of
* [key, value]
for each element in the Map)
*/
- entries(): ArrayValidator;
+ entries(): ArrayValidator<[K, V]>;
/**
* @param consumer - a function that accepts an {@link ArrayValidator} for the Map's entries (an
@@ -66,7 +69,7 @@ interface MapValidator extends ExtensibleObjectValidator
* @returns the updated validator
* @throws TypeError if consumer
is not set
*/
- entriesConsumer(consumer: (actual: ArrayValidator) => void): MapValidator;
+ entriesConsumer(consumer: (actual: ArrayValidator<[K, V]>) => void): MapValidator;
/**
* @returns a validator for the number of entries this Map contains
@@ -79,9 +82,27 @@ interface MapValidator extends ExtensibleObjectValidator
* @returns the updated validator
* @throws TypeError if consumer
is not set
*/
- sizeConsumer(consumer: (actual: NumberValidator) => void): MapValidator;
+ sizeConsumer(consumer: (actual: NumberValidator) => void): MapValidator;
+
+ /**
+ * @returns a validator for the Map
+ * @deprecated returns this
+ */
+ asMap(): MapValidator;
+
+ asMap(): MapValidator;
+
+ /**
+ * @param consumer - a function that accepts a {@link MapValidator} for the actual value
+ * @returns the updated validator
+ * @throws TypeError if consumer
is not set.
+ * If the actual value is not a Map
.
+ */
+ asMapConsumer(consumer: (input: MapValidator) => void): MapValidator;
+
+ asMapConsumer(consumer: (input: MapValidator) => void): MapValidator;
- getActual(): Map | void;
+ getActual(): Map | undefined;
}
export {type MapValidator};
\ No newline at end of file
diff --git a/src/MapVerifier.mts b/src/MapVerifier.mts
index babe61c..11788d4 100644
--- a/src/MapVerifier.mts
+++ b/src/MapVerifier.mts
@@ -8,8 +8,11 @@ import type {
* Verifies the requirements of a Map
.
*
* All methods (except those found in {@link ObjectVerifier}) imply {@link isNotNull}.
+ *
+ * @typeParam K - the type the map's keys
+ * @typeParam V - the type the map's values
*/
-interface MapVerifier extends ObjectVerifier
+interface MapVerifier extends ObjectVerifier