@vitruvius-labs/ts-predicate
Version:
TypeScript predicates library
22 lines (21 loc) • 979 B
JavaScript
import { hasNullishProperty } from "../extended/nullish/predicate/type-guard/has-nullish-property.mjs";
import { isNonNullableNullish } from "../utils/is-non-nullable-nullish.mjs";
import { itemAssertion } from "./utils/item-assertion.mjs";
import { ValidationError } from "./utils/validation-error.mjs";
import { isNullable } from "../type-guard/is-nullable.mjs";
function assertNullableProperty(value, property, test) {
if (!hasNullishProperty(value, property)) {
throw new ValidationError(`The value must have a property "${property.toString()}".`);
}
const property_value = value[property];
if (isNonNullableNullish(property_value)) {
throw new ValidationError(`The property "${property.toString()}" can be null or undefined, but cannot be NaN or NoValue.`);
}
if (isNullable(property_value)) {
return;
}
if (test !== undefined) {
itemAssertion(property_value, test);
}
}
export { assertNullableProperty };