Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sapphire/shapeshift

Package Overview
Dependencies
Maintainers
3
Versions
454
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sapphire/shapeshift - npm Package Compare versions

Comparing version 1.1.0-next.8727427.0 to 1.1.0-next.378c51f.0

30

dist/index.d.ts

@@ -59,2 +59,5 @@ declare class ConstraintError<T = unknown> extends Error {

or<O>(...predicates: readonly BaseValidator<O>[]): UnionValidator<T | O>;
transform(cb: (value: T) => T): this;
transform<O>(cb: (value: T) => O): BaseValidator<O>;
default(value: T | (() => T)): DefaultValidator<T>;
run(value: unknown): Result<T, Error>;

@@ -156,2 +159,10 @@ parse(value: unknown): T;

get negative(): this;
divisibleBy(divider: number): this;
get abs(): this;
get sign(): this;
get trunc(): this;
get floor(): this;
get fround(): this;
get round(): this;
get ceil(): this;
protected handle(value: unknown): Result<T, ValidationError>;

@@ -229,2 +240,18 @@ }

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 {

@@ -250,2 +277,3 @@ get string(): StringValidator<string>;

record<T>(validator: BaseValidator<T>): RecordValidator<T>;
map<T, U>(keyValidator: BaseValidator<T>, valueValidator: BaseValidator<U>): MapValidator<T, U>;
}

@@ -320,2 +348,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 };

161

dist/index.global.js

@@ -1,2 +0,1 @@

"use strict";
var SapphireShapeshift = (() => {

@@ -39,2 +38,32 @@ var __defProp = Object.defineProperty;

// src/lib/Result.ts
var Result = class {
constructor(success, value, error) {
this.success = success;
if (success) {
this.value = value;
} else {
this.error = error;
}
}
isOk() {
return this.success;
}
isErr() {
return !this.success;
}
unwrap() {
if (this.isOk())
return this.value;
throw this.error;
}
static ok(value) {
return new Result(true, value);
}
static err(error) {
return new Result(false, void 0, error);
}
};
__name(Result, "Result");
// src/validators/BaseValidator.ts

@@ -64,2 +93,8 @@ var BaseValidator = class {

}
transform(cb) {
return this.addConstraint({ run: (input) => Result.ok(cb(input)) });
}
default(value) {
return new DefaultValidator(this.clone(), value);
}
run(value) {

@@ -107,32 +142,2 @@ let result = this.handle(value);

// src/lib/Result.ts
var Result = class {
constructor(success, value, error) {
this.success = success;
if (success) {
this.value = value;
} else {
this.error = error;
}
}
isOk() {
return this.success;
}
isErr() {
return !this.success;
}
unwrap() {
if (this.isOk())
return this.value;
throw this.error;
}
static ok(value) {
return new Result(true, value);
}
static err(error) {
return new Result(false, void 0, error);
}
};
__name(Result, "Result");
// src/validators/ArrayValidator.ts

@@ -198,3 +203,3 @@ var ArrayValidator = class extends BaseValidator {

function ge(a, b) {
return a > b;
return a >= b;
}

@@ -448,2 +453,11 @@ __name(ge, "ge");

};
function numberDivisibleBy(divider) {
const expected = `% ${divider}`;
return {
run(input) {
return input % divider === 0 ? Result.ok(input) : Result.err(new ConstraintError("numberDivisibleBy", `Expected number to be divisible by ${divider}, but received ${input}`, input, expected));
}
};
}
__name(numberDivisibleBy, "numberDivisibleBy");

@@ -485,2 +499,26 @@ // src/validators/NumberValidator.ts

}
divisibleBy(divider) {
return this.addConstraint(numberDivisibleBy(divider));
}
get abs() {
return this.transform(Math.abs);
}
get sign() {
return this.transform(Math.sign);
}
get trunc() {
return this.transform(Math.trunc);
}
get floor() {
return this.transform(Math.floor);
}
get fround() {
return this.transform(Math.fround);
}
get round() {
return this.transform(Math.round);
}
get ceil() {
return this.transform(Math.ceil);
}
handle(value) {

@@ -677,3 +715,3 @@ return typeof value === "number" ? Result.ok(value) : Result.err(new ValidationError("NumberValidator", "Expected a number primitive", value));

if (!(values instanceof Set)) {
return Result.err(new ValidationError("ArrayValidator", "Expected an array", values));
return Result.err(new ValidationError("SetValidator", "Expected a set", values));
}

@@ -800,2 +838,56 @@ const errors = [];

// 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

@@ -862,2 +954,5 @@ var Shapes = class {

}
map(keyValidator, valueValidator) {
return new MapValidator(keyValidator, valueValidator);
}
};

@@ -864,0 +959,0 @@ __name(Shapes, "Shapes");

@@ -38,2 +38,32 @@ "use strict";

// src/lib/Result.ts
var Result = class {
constructor(success, value, error) {
this.success = success;
if (success) {
this.value = value;
} else {
this.error = error;
}
}
isOk() {
return this.success;
}
isErr() {
return !this.success;
}
unwrap() {
if (this.isOk())
return this.value;
throw this.error;
}
static ok(value) {
return new Result(true, value);
}
static err(error) {
return new Result(false, void 0, error);
}
};
__name(Result, "Result");
// src/validators/BaseValidator.ts

@@ -63,2 +93,8 @@ var BaseValidator = class {

}
transform(cb) {
return this.addConstraint({ run: (input) => Result.ok(cb(input)) });
}
default(value) {
return new DefaultValidator(this.clone(), value);
}
run(value) {

@@ -106,32 +142,2 @@ let result = this.handle(value);

// src/lib/Result.ts
var Result = class {
constructor(success, value, error) {
this.success = success;
if (success) {
this.value = value;
} else {
this.error = error;
}
}
isOk() {
return this.success;
}
isErr() {
return !this.success;
}
unwrap() {
if (this.isOk())
return this.value;
throw this.error;
}
static ok(value) {
return new Result(true, value);
}
static err(error) {
return new Result(false, void 0, error);
}
};
__name(Result, "Result");
// src/validators/ArrayValidator.ts

@@ -197,3 +203,3 @@ var ArrayValidator = class extends BaseValidator {

function ge(a, b) {
return a > b;
return a >= b;
}

@@ -447,2 +453,11 @@ __name(ge, "ge");

};
function numberDivisibleBy(divider) {
const expected = `% ${divider}`;
return {
run(input) {
return input % divider === 0 ? Result.ok(input) : Result.err(new ConstraintError("numberDivisibleBy", `Expected number to be divisible by ${divider}, but received ${input}`, input, expected));
}
};
}
__name(numberDivisibleBy, "numberDivisibleBy");

@@ -484,2 +499,26 @@ // src/validators/NumberValidator.ts

}
divisibleBy(divider) {
return this.addConstraint(numberDivisibleBy(divider));
}
get abs() {
return this.transform(Math.abs);
}
get sign() {
return this.transform(Math.sign);
}
get trunc() {
return this.transform(Math.trunc);
}
get floor() {
return this.transform(Math.floor);
}
get fround() {
return this.transform(Math.fround);
}
get round() {
return this.transform(Math.round);
}
get ceil() {
return this.transform(Math.ceil);
}
handle(value) {

@@ -676,3 +715,3 @@ return typeof value === "number" ? Result.ok(value) : Result.err(new ValidationError("NumberValidator", "Expected a number primitive", value));

if (!(values instanceof Set)) {
return Result.err(new ValidationError("ArrayValidator", "Expected an array", values));
return Result.err(new ValidationError("SetValidator", "Expected a set", values));
}

@@ -799,2 +838,56 @@ const errors = [];

// 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

@@ -861,2 +954,5 @@ var Shapes = class {

}
map(keyValidator, valueValidator) {
return new MapValidator(keyValidator, valueValidator);
}
};

@@ -863,0 +959,0 @@ __name(Shapes, "Shapes");

{
"name": "@sapphire/shapeshift",
"version": "1.1.0-next.8727427.0",
"version": "1.1.0-next.378c51f.0",
"description": "Blazing fast input validation and transformation ⚡",

@@ -5,0 +5,0 @@ "author": "@sapphire",

@@ -135,3 +135,3 @@ <div align="center">

s.number.divisibleBy(5); // TODO | Divisible by 5
s.number.divisibleBy(5); // Divisible by 5
```

@@ -142,10 +142,10 @@

```typescript
s.number.abs; // TODO | Transforms the number to an absolute number
s.number.sign; // TODO | Gets the number's sign
s.number.abs; // Transforms the number to an absolute number
s.number.sign; // Gets the number's sign
s.number.trunc; // TODO | Transforms the number to the result of Math.trunc`
s.number.floor; // TODO | Transforms the number to the result of Math.floor`
s.number.fround; // TODO | Transforms the number to the result of Math.fround`
s.number.round; // TODO | Transforms the number to the result of Math.round`
s.number.ceil; // TODO | Transforms the number to the result of Math.ceil`
s.number.trunc; // Transforms the number to the result of `Math.trunc`
s.number.floor; // Transforms the number to the result of `Math.floor`
s.number.fround; // Transforms the number to the result of `Math.fround`
s.number.round; // Transforms the number to the result of `Math.round`
s.number.ceil; // Transforms the number to the result of `Math.ceil`
```

@@ -366,3 +366,3 @@

#### Maps // TODO
#### Maps

@@ -425,3 +425,3 @@ ```typescript

const getLength = s.string.transform((value) => value.length); // TODO
const getLength = s.string.transform((value) => value.length);
getLength.parse('Hello There'); // => 11

@@ -446,3 +446,3 @@ ```

```typescript
const name = s.string.default('Sapphire'); // TODO
const name = s.string.default('Sapphire');
name.parse('Hello'); // => 'Hello'

@@ -453,3 +453,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

@@ -523,2 +523,3 @@ number.parse(undefined); // => 0.989911985608602

<td align="center"><a href="https://renovate.whitesourcesoftware.com/"><img src="https://avatars.githubusercontent.com/u/25180681?v=4?s=100" width="100px;" alt=""/><br /><sub><b>WhiteSource Renovate</b></sub></a><br /><a href="#maintenance-renovate-bot" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://github.com/Khasms"><img src="https://avatars.githubusercontent.com/u/36800359?v=4?s=100" width="100px;" alt=""/><br /><sub><b>John</b></sub></a><br /><a href="https://github.com/sapphiredev/shapeshift/commits?author=Khasms" title="Code">💻</a></td>
</tr>

@@ -525,0 +526,0 @@ </table>

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc