This commit is contained in:
Lukian 2023-06-20 15:28:07 +02:00
parent 68f4b60012
commit 41ae7ff4bd
1010 changed files with 38622 additions and 17071 deletions

View file

@ -1,5 +1,5 @@
import get from 'lodash/get.js';
import { inspect } from 'node:util';
import { inspect } from 'util';
import fastDeepEqual from 'fast-deep-equal/es6/index.js';
import uniqWith from 'lodash/uniqWith.js';
@ -181,6 +181,11 @@ var BaseValidator = class {
when(key, options) {
return this.addConstraint(whenConstraint(key, options, this));
}
describe(description) {
const clone = this.clone();
clone.description = description;
return clone;
}
run(value) {
let result = this.handle(value);
if (result.isErr())
@ -201,6 +206,11 @@ var BaseValidator = class {
is(value) {
return this.run(value).isOk();
}
/**
* Sets if the validator should also run constraints or just do basic checks.
* @param isValidationEnabled Whether this validator should be enabled or disabled. You can pass boolean or a function returning boolean which will be called just before parsing.
* Set to `null` to go off of the global configuration.
*/
setValidationEnabled(isValidationEnabled) {
const clone = this.clone();
clone.isValidationEnabled = isValidationEnabled;
@ -1090,7 +1100,7 @@ __name(UnionValidator, "UnionValidator");
// src/validators/ObjectValidator.ts
var ObjectValidator = class extends BaseValidator {
constructor(shape, strategy = ObjectValidatorStrategy.Ignore, constraints = []) {
constructor(shape, strategy = 0 /* Ignore */, constraints = []) {
super(constraints);
this.keys = [];
this.requiredKeys = /* @__PURE__ */ new Map();
@ -1099,14 +1109,14 @@ var ObjectValidator = class extends BaseValidator {
this.shape = shape;
this.strategy = strategy;
switch (this.strategy) {
case ObjectValidatorStrategy.Ignore:
case 0 /* Ignore */:
this.handleStrategy = (value) => this.handleIgnoreStrategy(value);
break;
case ObjectValidatorStrategy.Strict: {
case 1 /* Strict */: {
this.handleStrategy = (value) => this.handleStrictStrategy(value);
break;
}
case ObjectValidatorStrategy.Passthrough:
case 2 /* Passthrough */:
this.handleStrategy = (value) => this.handlePassthroughStrategy(value);
break;
}
@ -1144,13 +1154,13 @@ var ObjectValidator = class extends BaseValidator {
}
}
get strict() {
return Reflect.construct(this.constructor, [this.shape, ObjectValidatorStrategy.Strict, this.constraints]);
return Reflect.construct(this.constructor, [this.shape, 1 /* Strict */, this.constraints]);
}
get ignore() {
return Reflect.construct(this.constructor, [this.shape, ObjectValidatorStrategy.Ignore, this.constraints]);
return Reflect.construct(this.constructor, [this.shape, 0 /* Ignore */, this.constraints]);
}
get passthrough() {
return Reflect.construct(this.constructor, [this.shape, ObjectValidatorStrategy.Passthrough, this.constraints]);
return Reflect.construct(this.constructor, [this.shape, 2 /* Passthrough */, this.constraints]);
}
get partial() {
const shape = Object.fromEntries(this.keys.map((key) => [key, this.shape[key].optional]));
@ -1294,12 +1304,6 @@ var ObjectValidator = class extends BaseValidator {
}
};
__name(ObjectValidator, "ObjectValidator");
var ObjectValidatorStrategy = /* @__PURE__ */ ((ObjectValidatorStrategy2) => {
ObjectValidatorStrategy2[ObjectValidatorStrategy2["Ignore"] = 0] = "Ignore";
ObjectValidatorStrategy2[ObjectValidatorStrategy2["Strict"] = 1] = "Strict";
ObjectValidatorStrategy2[ObjectValidatorStrategy2["Passthrough"] = 2] = "Passthrough";
return ObjectValidatorStrategy2;
})(ObjectValidatorStrategy || {});
// src/validators/PassthroughValidator.ts
var PassthroughValidator = class extends BaseValidator {
@ -1590,7 +1594,7 @@ function stringUrl(options) {
try {
url = new URL(input);
} catch {
return Result.err(new ExpectedConstraintError("s.string.url", "Invalid URL", input, "expected to match an URL"));
return Result.err(new ExpectedConstraintError("s.string.url", "Invalid URL", input, "expected to match a URL"));
}
const validatorFnResult = validatorFn(input, url);
if (validatorFnResult === null)