@sapphire/shapeshift
Advanced tools
Comparing version 1.1.0-next.878a0f6.0 to 1.1.0-next.88d82ed.0
@@ -61,2 +61,3 @@ declare class ConstraintError<T = unknown> extends Error { | ||
transform<O>(cb: (value: T) => O): BaseValidator<O>; | ||
default(value: T | (() => T)): DefaultValidator<T>; | ||
run(value: unknown): Result<T, Error>; | ||
@@ -238,2 +239,18 @@ parse(value: unknown): T; | ||
declare class MapValidator<K, V> extends BaseValidator<Map<K, V>> { | ||
private readonly keyValidator; | ||
private readonly valueValidator; | ||
constructor(keyValidator: BaseValidator<K>, valueValidator: BaseValidator<V>, constraints?: readonly IConstraint<Map<K, V>>[]); | ||
protected clone(): this; | ||
protected handle(value: unknown): Result<Map<K, V>, ValidationError | AggregateError>; | ||
} | ||
declare class DefaultValidator<T> extends BaseValidator<T | undefined> { | ||
private readonly validator; | ||
private readonly defaultValue; | ||
constructor(validator: BaseValidator<T>, value: T | (() => T), constraints?: readonly IConstraint<T>[]); | ||
protected handle(value: unknown): Result<T, ValidationError | AggregateError>; | ||
protected clone(): this; | ||
} | ||
declare class Shapes { | ||
@@ -259,2 +276,3 @@ get string(): StringValidator<string>; | ||
record<T>(validator: BaseValidator<T>): RecordValidator<T>; | ||
map<T, U>(keyValidator: BaseValidator<T>, valueValidator: BaseValidator<U>): MapValidator<T, U>; | ||
} | ||
@@ -329,2 +347,2 @@ | ||
export { ArrayValidator, BaseValidator, BigIntValidator, BooleanValidator, ConstraintError, ConstraintErrorMessageBuilder, Constructor, DateValidator, ExpectedValidationError, IConstraint, InstanceValidator, LiteralValidator, MappedObjectValidator, MissingPropertyError, NeverValidator, NonNullObject, NullishValidator, NumberValidator, ObjectValidator, ObjectValidatorStrategy, PassthroughValidator, RecordValidator, Result, SetValidator, Shapes, StringValidator, Type, UnionValidator, UnknownPropertyError, ValidationError, arrayLengthEq, arrayLengthGe, arrayLengthGt, arrayLengthLe, arrayLengthLt, arrayLengthNe, bigintEq, bigintGe, bigintGt, bigintLe, bigintLt, bigintNe, booleanFalse, booleanTrue, dateEq, dateGe, dateGt, dateInvalid, dateLe, dateLt, dateNe, dateValid, numberEq, numberFinite, numberGe, numberGt, numberInt, numberLe, numberLt, numberNaN, numberNe, numberNeNaN, numberSafeInt, s, stringLengthEq, stringLengthGe, stringLengthGt, stringLengthLe, stringLengthLt, stringLengthNe }; | ||
export { ArrayValidator, BaseValidator, BigIntValidator, BooleanValidator, ConstraintError, ConstraintErrorMessageBuilder, Constructor, DateValidator, DefaultValidator, ExpectedValidationError, IConstraint, InstanceValidator, LiteralValidator, MapValidator, MappedObjectValidator, MissingPropertyError, NeverValidator, NonNullObject, NullishValidator, NumberValidator, ObjectValidator, ObjectValidatorStrategy, PassthroughValidator, RecordValidator, Result, SetValidator, Shapes, StringValidator, Type, UnionValidator, UnknownPropertyError, ValidationError, arrayLengthEq, arrayLengthGe, arrayLengthGt, arrayLengthLe, arrayLengthLt, arrayLengthNe, bigintEq, bigintGe, bigintGt, bigintLe, bigintLt, bigintNe, booleanFalse, booleanTrue, dateEq, dateGe, dateGt, dateInvalid, dateLe, dateLt, dateNe, dateValid, numberEq, numberFinite, numberGe, numberGt, numberInt, numberLe, numberLt, numberNaN, numberNe, numberNeNaN, numberSafeInt, s, stringLengthEq, stringLengthGe, stringLengthGt, stringLengthLe, stringLengthLt, stringLengthNe }; |
@@ -95,2 +95,5 @@ var SapphireShapeshift = (() => { | ||
} | ||
default(value) { | ||
return new DefaultValidator(this.clone(), value); | ||
} | ||
run(value) { | ||
@@ -829,2 +832,56 @@ let result = this.handle(value); | ||
// src/validators/MapValidator.ts | ||
var MapValidator = class extends BaseValidator { | ||
constructor(keyValidator, valueValidator, constraints = []) { | ||
super(constraints); | ||
this.keyValidator = keyValidator; | ||
this.valueValidator = valueValidator; | ||
} | ||
clone() { | ||
return Reflect.construct(this.constructor, [this.keyValidator, this.valueValidator, this.constraints]); | ||
} | ||
handle(value) { | ||
if (!(value instanceof Map)) { | ||
return Result.err(new ValidationError("MapValidator", "Expected a map", value)); | ||
} | ||
const errors = []; | ||
const transformed = /* @__PURE__ */ new Map(); | ||
for (const [key, val] of value.entries()) { | ||
const keyResult = this.keyValidator.run(key); | ||
const valueResult = this.valueValidator.run(val); | ||
const { length } = errors; | ||
if (keyResult.isErr()) | ||
errors.push(keyResult.error); | ||
if (valueResult.isErr()) | ||
errors.push(valueResult.error); | ||
if (errors.length === length) | ||
transformed.set(keyResult.value, valueResult.value); | ||
} | ||
return errors.length === 0 ? Result.ok(transformed) : Result.err(new AggregateError(errors, "Failed to validate at least one entry")); | ||
} | ||
}; | ||
__name(MapValidator, "MapValidator"); | ||
// src/validators/util/getValue.ts | ||
function getValue(valueOrFn) { | ||
return typeof valueOrFn === "function" ? valueOrFn() : valueOrFn; | ||
} | ||
__name(getValue, "getValue"); | ||
// src/validators/DefaultValidator.ts | ||
var DefaultValidator = class extends BaseValidator { | ||
constructor(validator, value, constraints = []) { | ||
super(constraints); | ||
this.validator = validator; | ||
this.defaultValue = value; | ||
} | ||
handle(value) { | ||
return typeof value === "undefined" ? Result.ok(getValue(this.defaultValue)) : this.validator["handle"](value); | ||
} | ||
clone() { | ||
return Reflect.construct(this.constructor, [this.validator, this.defaultValue, this.constraints]); | ||
} | ||
}; | ||
__name(DefaultValidator, "DefaultValidator"); | ||
// src/lib/Shapes.ts | ||
@@ -891,2 +948,5 @@ var Shapes = class { | ||
} | ||
map(keyValidator, valueValidator) { | ||
return new MapValidator(keyValidator, valueValidator); | ||
} | ||
}; | ||
@@ -893,0 +953,0 @@ __name(Shapes, "Shapes"); |
@@ -95,2 +95,5 @@ "use strict"; | ||
} | ||
default(value) { | ||
return new DefaultValidator(this.clone(), value); | ||
} | ||
run(value) { | ||
@@ -829,2 +832,56 @@ let result = this.handle(value); | ||
// src/validators/MapValidator.ts | ||
var MapValidator = class extends BaseValidator { | ||
constructor(keyValidator, valueValidator, constraints = []) { | ||
super(constraints); | ||
this.keyValidator = keyValidator; | ||
this.valueValidator = valueValidator; | ||
} | ||
clone() { | ||
return Reflect.construct(this.constructor, [this.keyValidator, this.valueValidator, this.constraints]); | ||
} | ||
handle(value) { | ||
if (!(value instanceof Map)) { | ||
return Result.err(new ValidationError("MapValidator", "Expected a map", value)); | ||
} | ||
const errors = []; | ||
const transformed = /* @__PURE__ */ new Map(); | ||
for (const [key, val] of value.entries()) { | ||
const keyResult = this.keyValidator.run(key); | ||
const valueResult = this.valueValidator.run(val); | ||
const { length } = errors; | ||
if (keyResult.isErr()) | ||
errors.push(keyResult.error); | ||
if (valueResult.isErr()) | ||
errors.push(valueResult.error); | ||
if (errors.length === length) | ||
transformed.set(keyResult.value, valueResult.value); | ||
} | ||
return errors.length === 0 ? Result.ok(transformed) : Result.err(new AggregateError(errors, "Failed to validate at least one entry")); | ||
} | ||
}; | ||
__name(MapValidator, "MapValidator"); | ||
// src/validators/util/getValue.ts | ||
function getValue(valueOrFn) { | ||
return typeof valueOrFn === "function" ? valueOrFn() : valueOrFn; | ||
} | ||
__name(getValue, "getValue"); | ||
// src/validators/DefaultValidator.ts | ||
var DefaultValidator = class extends BaseValidator { | ||
constructor(validator, value, constraints = []) { | ||
super(constraints); | ||
this.validator = validator; | ||
this.defaultValue = value; | ||
} | ||
handle(value) { | ||
return typeof value === "undefined" ? Result.ok(getValue(this.defaultValue)) : this.validator["handle"](value); | ||
} | ||
clone() { | ||
return Reflect.construct(this.constructor, [this.validator, this.defaultValue, this.constraints]); | ||
} | ||
}; | ||
__name(DefaultValidator, "DefaultValidator"); | ||
// src/lib/Shapes.ts | ||
@@ -891,2 +948,5 @@ var Shapes = class { | ||
} | ||
map(keyValidator, valueValidator) { | ||
return new MapValidator(keyValidator, valueValidator); | ||
} | ||
}; | ||
@@ -893,0 +953,0 @@ __name(Shapes, "Shapes"); |
{ | ||
"name": "@sapphire/shapeshift", | ||
"version": "1.1.0-next.878a0f6.0", | ||
"version": "1.1.0-next.88d82ed.0", | ||
"description": "Blazing fast input validation and transformation ⚡", | ||
@@ -37,11 +37,11 @@ "author": "@sapphire", | ||
"@favware/rollup-type-bundler": "^1.0.7", | ||
"@sapphire/eslint-config": "^4.0.11", | ||
"@sapphire/prettier-config": "^1.2.9", | ||
"@sapphire/ts-config": "^3.1.8", | ||
"@sapphire/eslint-config": "^4.1.0", | ||
"@sapphire/prettier-config": "^1.3.0", | ||
"@sapphire/ts-config": "^3.2.0", | ||
"@types/jest": "^27.4.0", | ||
"@types/node": "^17.0.8", | ||
"@typescript-eslint/eslint-plugin": "^5.10.0", | ||
"@typescript-eslint/parser": "^5.10.0", | ||
"@typescript-eslint/eslint-plugin": "^5.10.1", | ||
"@typescript-eslint/parser": "^5.10.1", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"eslint": "~8.7.0", | ||
"eslint": "~8.8.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
@@ -52,3 +52,3 @@ "eslint-plugin-prettier": "^4.0.0", | ||
"jest-circus": "^27.4.6", | ||
"lint-staged": "^12.2.2", | ||
"lint-staged": "^12.3.2", | ||
"prettier": "^2.5.1", | ||
@@ -59,5 +59,5 @@ "pretty-quick": "^3.1.3", | ||
"ts-node": "^10.4.0", | ||
"tsup": "^5.11.11", | ||
"tsup": "^5.11.13", | ||
"typedoc": "^0.22.11", | ||
"typedoc-plugin-mdn-links": "^1.0.4", | ||
"typedoc-plugin-mdn-links": "^1.0.5", | ||
"typescript": "^4.5.5" | ||
@@ -64,0 +64,0 @@ }, |
@@ -364,3 +364,3 @@ <div align="center"> | ||
#### Maps // TODO | ||
#### Maps | ||
@@ -443,3 +443,3 @@ ```typescript | ||
```typescript | ||
const name = s.string.default('Sapphire'); // TODO | ||
const name = s.string.default('Sapphire'); | ||
name.parse('Hello'); // => 'Hello' | ||
@@ -450,3 +450,3 @@ name.parse(undefined); // => 'Sapphire' | ||
```typescript | ||
const number = s.number.default(Math.random); // TODO | ||
const number = s.number.default(Math.random); | ||
number.parse(12); // => 12 | ||
@@ -453,0 +453,0 @@ number.parse(undefined); // => 0.989911985608602 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
339793
3059