- type Article: "a" | "an"
No description.
- type Type: "array" | "NaN" | "null" | "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
Extended
typeof
. Used inRuntimeTypeCheck.getType
.
- type Descriptor: ConditionList[]
List of possible conditions, any of which may match. Thus, it is a disjunction of conjunctions (OR list of AND lists).
- type ConditionList: Condition | Condition[]
Either a single condition that must match, or a list of conditions, all of which must match.
class Cond
- static array: (...descriptor: Descriptor) => Condition & Condition
Generate a condition that asserts a value to be an array, optionally with the given descriptor inside it.
This function itself is a condition without inner types, so it can be used as
Cond.array
as an alias toCond.array()
.- static object: (keyName?: string, ...descriptor: Descriptor) => Condition & Condition
Generate a condition that asserts a value to be an object literal, optionally with the given descriptor inside it.
This function itself is a condition without inner types, so it can be used as
Cond.object
as an alias toCond.object()
.Param (keyName)
A concise key description used when displaying the type:
Object<keyName, ...>
.Generate a condition that asserts a value to be only the specified strings. Implies
string
.
class TypeCheckError extends Error
- TypeCheckError(expected: string, is: string): TypeCheckError
No description.
- expected: string
No description.
- is: string
No description.
class RuntimeTypeCheck
No description.
- static assertAndThrow(val: any,): boolean
...descriptor: Descriptor Assert an arbitrary value to match any of the given conditions and throw a detailed explanatory error message if the assertion fails.
The conditions are tested recursively through their potential
Condition.conditions
field.Parameters
-
val
: The value to test. -
descriptor
: The conditions to test the value against.
-
- static assert(val: any, ...descriptor: Descriptor): boolean
Assert an arbitrary value to match any of of the given conditions.
The conditions are tested recursively through their potential
Condition.conditions
field.Parameters
-
val
: The value to test. -
descriptor
: The conditions to test the value against.
-
- static assertFind(val: any,): undefined | Condition
...descriptor: Descriptor If a given arbitrary value does not assert any of the given conditions, return the most relevant failing condition in the context of the given value.
The conditions are tested recursively through their potential
Condition.conditions
field.Parameters
-
val
: The value to test. -
descriptor
: The conditions to test the value against.
Remarks
This method probably doesn't have to concern you. It is used within
getMessageIs
to get the most relevant condition for the resulting message.Example
assertFind(3.2, Cond.string, [ Cond.positive, Cond.integer ], Cond.true);
Returns
Cond.integer
because it is the most relevant failing condition for the value3.2
.-
- static getMostRelevantFailingCondition(val: any,): undefined | Condition
...descriptor: Descriptor Recursively count passing conditions, weighted by shallowness, and return the failing condition in the context of the maximum pass count.
In other words, find the failing condition whose sibling and children conditions pass the most amount of assertions for the given value and are thus the most relevant condition context.
This does not check if a descriptor as a whole asserts to true.
See also
assertFind
Internal
- static getMessageIsIterated(val: any[],): undefined | string
...descriptor: Descriptor Return the result of
getMessageIs
for the first of the passed values that does not assert.Parameters
-
val
: An array of the values to test. -
descriptor
: The conditions to test the value against.
-
- static getMessageIs(val: any,): string
...descriptor: Descriptor Return a string denoting the type of the value in the context of the condition that is closest to matching, or an empty string if all conditions assert correctly.
The returned string is simply the result of executing the
Condition.is
field of the failing condition.Parameters
-
val
: The value to test. -
descriptor
: The conditions to test the value against. The first failed condition will produce the return value.
Example
Value: `0` Conditions: `[ Cond.number, Cond.positive ]` Return value: `"a negative number or 0"`
-
- static getMessageExpected(...descriptor: Descriptor): string
Return a string denoting the expected type within the passed conditions. The message is created by recursively merging all
shouldBe
fields.Example
descriptor 1: `[ Cond.number, Cond.positive ]` descriptor 2: `Cond.keywords("foobar")` Return value: `'positive number OR the keyword "foobar"'`
- static compilePartialMessage(messagePartial: MessagePartial): string
Compile a single
MessagePartial
into a coherent sentence of the form "[...before] [type] [...after]". A few linguistic transformations are made.Example
Input:
{ before: [ 'nonverbal', 'positive' ], type: [ 'integer' ], after: [ 'that is cool', 'with 6 digits', 'that is divisible by 5' ] }
Output:
"nonverbal, positive integer with 6 digits that is cool and is divisible by 5"
Internal
- static mergeDescriptorMessages(...descriptor: Descriptor): MessagePartial[]
Recursively look at the messages defined in the
shouldBe
field of the given descriptors and merge them from bottom to top, returning a disjunction ofMessagePartial
s.Disjunction means that every output list item is a standalone message for a seperate assertion (OR).
Internal
- static getArticle(value: string): "a" | "an"
Get the matching indefinite article (a or an) for the passed string.
Remarks
This method is by no means linguistically sound, it simply checks whether the first character of the passed string is a vowel.
- static getPrettyEnumeratedList(list: string[]): string
Get a string list of all items of the passed string array of the style "first, second, third or fourth".
Remarks
This is used within the
RuntimeTypeCheck.Cond.keywords
condition.
interface IsData
Data describing attributes of a value that did not pass
a condition's assertion. Utilized in Condition.is
.
- val: any
No description.
interface ExpectedData
- type: string
No description.
interface Message
Contains the three parts of the message used to denote the expected type of the form "[before] [type] [after]".
Message parts from extended conditions (via Condition.conditions
)
will be inherited and merged.
Example
Extending RuntimeTypeCheck.Cond.number
will inherit its
type
as "number"
. Building a before: "positive"
on top of
it will create the following Message:
{
before: 'positive',
type: 'number',
}
And yield the following sentence: "Expected positive number [...]"
.
interface Condition
Further conditions that this condition relies on. The passed value in
assert
is ensured to match these conditions.Note that this is a
Descriptor
, so it is an OR list of AND lists.For example, a condition asserting a positive number may specify the condition of type number.
- assert: (value: any) => boolean
Assertion function for an arbitrary value.
The passed value is ensured to match the specified
Descriptor
inCondition.conditions
, if any.Denote what the expected value should be.
See also
Message
.Example
Assuming the condition asserts a positive number and specifies the further condition
RuntimeTypeCheck.Cond.number
. The attribute "positive" can then simply be added to the front of the type. The type itself ("number") is contributed by the extended condition, so we don't have to care about that:shouldBe: { before: 'positive' }
The final error message will then be: "Expected positive number, got [...]."
Sentence denoting what the value is when it does not assert. In simple cases, this is usually a description of the opposite of the assertion. The sentence should always start with a lowercase indefinite article (a or an).
For advanced use cases, a function may be specified which takes an
IsData
object that contains useful information about the value in question.Example
Simple Assuming the condition asserts a positive number, the `is` value could be "a negative number or 0", which is a description of the exact opposite of the expected value.
The final error message will then be: "Expected [...], got a negative number or 0."
Example (Advanced)
Assuming the condition asserts the length of a value and the specified further conditions are therefore
RuntimeTypeCheck.Cond.array
andRuntimeTypeCheck.Cond.string
.The type or value of the value are unknown, so we can utilize the method to get type and article and append a sentence describing the value:
({ type, article }) => `${article} ${type} of a different length`.
In case of an array, the final error message will then be: "Expected [...], got an array of a different length"