@sapphire/shapeshift
Advanced tools
Comparing version 1.1.0-next.89a8ddd.0 to 1.1.0-next.c4d1258.0
@@ -237,2 +237,10 @@ declare class ConstraintError<T = unknown> extends Error { | ||
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 Shapes { | ||
@@ -258,2 +266,3 @@ get string(): StringValidator<string>; | ||
record<T>(validator: BaseValidator<T>): RecordValidator<T>; | ||
map<T, U>(keyValidator: BaseValidator<T>, valueValidator: BaseValidator<U>): MapValidator<T, U>; | ||
} | ||
@@ -328,2 +337,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, 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 }; |
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
var SapphireShapeshift = (() => { | ||
@@ -829,2 +828,34 @@ var __defProp = Object.defineProperty; | ||
// 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/lib/Shapes.ts | ||
@@ -891,2 +922,5 @@ var Shapes = class { | ||
} | ||
map(keyValidator, valueValidator) { | ||
return new MapValidator(keyValidator, valueValidator); | ||
} | ||
}; | ||
@@ -893,0 +927,0 @@ __name(Shapes, "Shapes"); |
@@ -828,2 +828,34 @@ "use strict"; | ||
// 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/lib/Shapes.ts | ||
@@ -890,2 +922,5 @@ var Shapes = class { | ||
} | ||
map(keyValidator, valueValidator) { | ||
return new MapValidator(keyValidator, valueValidator); | ||
} | ||
}; | ||
@@ -892,0 +927,0 @@ __name(Shapes, "Shapes"); |
{ | ||
"name": "@sapphire/shapeshift", | ||
"version": "1.1.0-next.89a8ddd.0", | ||
"version": "1.1.0-next.c4d1258.0", | ||
"description": "Blazing fast input validation and transformation ⚡", | ||
@@ -5,0 +5,0 @@ "author": "@sapphire", |
@@ -364,3 +364,3 @@ <div align="center"> | ||
#### Maps // TODO | ||
#### Maps | ||
@@ -367,0 +367,0 @@ ```typescript |
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
330992
2982