@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
76 lines (74 loc) • 3.13 kB
JavaScript
import {
inFuture,
inPast
} from "./chunk-ADJAEGCT.mjs";
import {
tryTo,
validate
} from "./chunk-XAIHCZT4.mjs";
import {
meta
} from "./chunk-WTFW6DLP.mjs";
import {
text
} from "./chunk-MCCIBDEH.mjs";
import {
toList
} from "./chunk-A7C3XND3.mjs";
import {
isBoolean,
isDefined,
isFunction,
isIn,
isNotEmpty,
isString
} from "./chunk-DEJ7A5PY.mjs";
// src/validation/Contraints.ts
var constraint = (c, message) => (subject, property) => {
const cs = meta(subject).property(property).get("constraint") ?? toList();
meta(subject).property(property).set("constraint", cs.add({ property, constraint: c, text: message }));
};
var defined = (message) => constraint((v) => isDefined(v), message ?? "Property {property} must be defined.");
var required = (message) => constraint((v) => isNotEmpty(v), message ?? "Property {property} is required, and may not be empty.");
var notEmpty = (message) => constraint((v) => isNotEmpty(v), message ?? "Property {property} may not be empty.");
var valid = () => constraint((v) => validate(v), "");
var optional = () => constraint((v) => !isDefined(v) || validate(v), "");
var includes = (sub, message) => constraint((s) => isDefined(s) && isString(s) && s.includes(sub), message ?? `Value {actual} must include '${sub}'.`);
var inList = (values, message) => constraint((v) => isDefined(v) && isIn(v, values), message ?? "Value {actual} must appear in list.");
var inOptionalList = (values, message) => constraint((v) => !isDefined(v) || isIn(v, values), message ?? "Value {actual} must appear in list.");
var gt = (limit, message) => constraint((v) => v > limit, message ?? `Value {actual} must be larger than '${limit}'.`);
var gte = (limit, message) => constraint((v) => v >= limit, message ?? `Value {actual} must be larger than or equal to ${limit}.`);
var lt = (limit, message) => constraint((v) => v < limit, message ?? `Value {actual} must be smaller than ${limit}.`);
var lte = (limit, message) => constraint((v) => v <= limit, message ?? `Value {actual} must be smaller than or equal to ${limit}.`);
var past = (message) => constraint((v) => inPast(v), message ?? "Value {actual} must lay in the past.");
var future = (message) => constraint((v) => inFuture(v), message ?? "Value {actual} must lay in the future.");
var minLength = (length, message) => constraint(
(v) => tryTo(() => v).is.defined().map((v2) => text(v2).toString().length >= length).orElse(true),
message ?? `Value {actual} must be at least '${length}' characters long.`
);
var maxLength = (length, message) => constraint(
(v) => tryTo(() => v).is.defined().map((v2) => text(v2).toString().length <= length).orElse(true),
message ?? `Value {actual} cannot be longer than '${length}' characters.`
);
var rule = (message) => constraint((v) => isFunction(v) ? v() : isBoolean(v) ? v : false, message ?? `Value {actual} must be true`);
export {
constraint,
defined,
required,
notEmpty,
valid,
optional,
includes,
inList,
inOptionalList,
gt,
gte,
lt,
lte,
past,
future,
minLength,
maxLength,
rule
};
//# sourceMappingURL=chunk-WDDAMKGZ.mjs.map