Coder Social home page Coder Social logo

sapphiredev / shapeshift Goto Github PK

View Code? Open in Web Editor NEW
92.0 5.0 13.0 15.89 MB

Blazing fast input validation and transformation ⚡

Home Page: https://www.sapphirejs.dev

License: MIT License

TypeScript 99.76% JavaScript 0.24%
input-validation schema validation runtime-validation type-validation type-checking checking zod ow hacktoberfest

shapeshift's Introduction

Sapphire Logo

@sapphire/shapeshift

Shapeshift

Blazing fast input validation and transformation ⚡

GitHub codecov npm

Table of Contents

Description

Back to top

A very fast and lightweight input validation and transformation library for JavaScript.

Note: Shapeshift requires Node.js v14.0.0 or higher to work.

Features

Back to top

  • TypeScript friendly
  • Offers CJS, ESM and UMD builds
  • API similar to zod and yup
  • Faster than ⚡

Usage

Back to top

For complete usages, please dive into our documentation

Basic usage

Back to top

Creating a simple string validation

import { s } from '@sapphire/shapeshift';

const myStringValidation = s.string();

// Parse
myStringValidation.parse('sapphire'); // => returns 'sapphire'
myStringValidation.parse(12); // throws ValidationError

Creating an object schema

import { s } from '@sapphire/shapeshift';

const user = s.object({
  username: s.string()
});

user.parse({ username: 'Sapphire' });

Defining validations

Back to top

Primitives

Back to top

import { s } from '@sapphire/shapeshift';

// Primitives
s.string();
s.number();
s.bigint();
s.boolean();
s.date();

// Empty Types
s.undefined();
s.null();
s.nullish(); // Accepts undefined | null

// Catch-all Types
s.any();
s.unknown();

// Never Type
s.never();

Literals

Back to top

s.literal('sapphire');
s.literal(12);
s.literal(420n);
s.literal(true);
s.literal(new Date(1639278160000)); // s.date().equal(1639278160000);

Strings

Back to top

Shapeshift includes a handful of string-specific validations:

s.string().lengthLessThan(5);
s.string().lengthLessThanOrEqual(5);
s.string().lengthGreaterThan(5);
s.string().lengthGreaterThanOrEqual(5);
s.string().lengthEqual(5);
s.string().lengthNotEqual(5);
s.string().email();
s.string().url();
s.string().uuid();
s.string().regex(regex);
s.string().ip();
s.string().ipv4();
s.string().ipv6();
s.string().phone();

Numbers

Back to top

Shapeshift includes a handful of number-specific validations:

s.number().greaterThan(5); // > 5
s.number().greaterThanOrEqual(5); // >= 5
s.number().lessThan(5); // < 5
s.number().lessThanOrEqual(5); // <= 5
s.number().equal(5); // === 5
s.number().notEqual(5); // !== 5

s.number().equal(NaN); // special case: Number.isNaN
s.number().notEqual(NaN); // special case: !Number.isNaN

s.number().int(); // value must be an integer
s.number().safeInt(); // value must be a safe integer
s.number().finite(); // value must be finite

s.number().positive(); // .greaterThanOrEqual(0)
s.number().negative(); // .lessThan(0)

s.number().divisibleBy(5); // Divisible by 5

And transformations:

s.number().abs(); // Transforms the number to an absolute number
s.number().sign(); // Gets the number's sign

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`

BigInts

Back to top

Shapeshift includes a handful of number-specific validations:

s.bigint().greaterThan(5n); // > 5n
s.bigint().greaterThanOrEqual(5n); // >= 5n
s.bigint().lessThan(5n); // < 5n
s.bigint().lessThanOrEqual(5n); // <= 5n
s.bigint().equal(5n); // === 5n
s.bigint().notEqual(5n); // !== 5n

s.bigint().positive(); // .greaterThanOrEqual(0n)
s.bigint().negative(); // .lessThan(0n)

s.bigint().divisibleBy(5n); // Divisible by 5n

And transformations:

s.bigint().abs(); // Transforms the bigint to an absolute bigint

s.bigint().intN(5); // Clamps to a bigint to a signed bigint with 5 digits, see BigInt.asIntN
s.bigint().uintN(5); // Clamps to a bigint to an unsigned bigint with 5 digits, see BigInt.asUintN

Booleans

Back to top

Shapeshift includes a few boolean-specific validations:

s.boolean().true(); // value must be true
s.boolean().false(); // value must be false

s.boolean().equal(true); // s.boolean().true()
s.boolean().equal(false); // s.boolean().false()

s.boolean().notEqual(true); // s.boolean().false()
s.boolean().notEqual(false); // s.boolean().true()

Arrays

Back to top

const stringArray = s.array(s.string());
const stringArray = s.string().array();

Shapeshift includes a handful of array-specific validations:

s.string().array().lengthLessThan(5); // Must have less than 5 elements
s.string().array().lengthLessThanOrEqual(5); // Must have 5 or less elements
s.string().array().lengthGreaterThan(5); // Must have more than 5 elements
s.string().array().lengthGreaterThanOrEqual(5); // Must have 5 or more elements
s.string().array().lengthEqual(5); // Must have exactly 5 elements
s.string().array().lengthNotEqual(5); // Must not have exactly 5 elements
s.string().array().lengthRange(0, 4); // Must have at least 0 elements and less than 4 elements (in math, that is [0, 4))
s.string().array().lengthRangeInclusive(0, 4); // Must have at least 0 elements and at most 4 elements (in math, that is [0, 4])
s.string().array().lengthRangeExclusive(0, 4); // Must have more than 0 element and less than 4 elements (in math, that is (0, 4))
s.string().array().unique(); // All elements must be unique. Deep equality is used to check for uniqueness.

Note: All .length methods define tuple types with the given amount of elements. For example, s.string().array().lengthGreaterThanOrEqual(2)'s inferred type is [string, string, ...string[]]

Tuples

Back to top

Unlike arrays, tuples have a fixed number of elements and each element can have a different type:

const dish = s.tuple([
  s.string(), // Dish's name
  s.number().int(), // Table's number
  s.date() // Date the dish was ready for delivery
]);

dish.parse(['Iberian ham', 10, new Date()]);

Unions

Back to top

Shapeshift includes a built-in method for composing OR types:

const stringOrNumber = s.union([s.string(), s.number()]);

stringOrNumber.parse('Sapphire'); // => 'Sapphire'
stringOrNumber.parse(42); // => 42
stringOrNumber.parse({}); // => throws CombinedError

Enums

Back to top

Enums are a convenience method that aliases s.union([s.literal(a), s.literal(b), ...]):

s.enum(['Red', 'Green', 'Blue']);
// s.union([s.literal('Red'), s.literal('Green'), s.literal('Blue')]);

Maps

Back to top

const map = s.map(s.string(), s.number());
// Map<string, number>

Sets

Back to top

const set = s.set(s.number());
// Set<number>

Instances

Back to top

You can use s.instance(Class) to check that the input is an instance of a class. This is useful to validate inputs against classes:

class User {
  public constructor(public name: string) {}
}

const userInstanceValidation = s.instance(User);
userInstanceValidation.parse(new User('Sapphire')); // => User { name: 'Sapphire' }
userInstanceValidation.parse('oops'); // => throws ValidatorError

Records

Back to top

Record validations are similar to objects, but validate Record<string, T> types. Keep in mind this does not check for the keys, and cannot support validation for specific ones:

const tags = s.record(s.string());

tags.parse({ foo: 'bar', hello: 'world' }); // => { foo: 'bar', hello: 'world' }
tags.parse({ foo: 42 }); // => throws CombinedError
tags.parse('Hello'); // => throws ValidateError

TypedArray

Back to top

const typedArray = s.typedArray();
const int16Array = s.int16Array();
const uint16Array = s.uint16Array();
const uint8ClampedArray = s.uint8ClampedArray();
const int16Array = s.int16Array();
const uint16Array = s.uint16Array();
const int32Array = s.int32Array();
const uint32Array = s.uint32Array();
const float32Array = s.float32Array();
const float64Array = s.float64Array();
const bigInt64Array = s.bigInt64Array();
const bigUint64Array = s.bigUint64Array();

Shapeshift includes a handful of validations specific to typed arrays.

s.typedArray().lengthLessThan(5); // Length must be less than 5
s.typedArray().lengthLessThanOrEqual(5); // Length must be 5 or less
s.typedArray().lengthGreaterThan(5); // Length must be more than 5
s.typedArray().lengthGreaterThanOrEqual(5); // Length must be 5 or more
s.typedArray().lengthEqual(5); // Length must be exactly 5
s.typedArray().lengthNotEqual(5); // Length must not be 5
s.typedArray().lengthRange(0, 4); // Length L must satisfy 0 <= L < 4
s.typedArray().lengthRangeInclusive(0, 4); // Length L must satisfy 0 <= L <= 4
s.typedArray().lengthRangeExclusive(0, 4); // Length L must satisfy 0 < L < 4

Note that all of these methods have analogous methods for working with the typed array's byte length, s.typedArray().byteLengthX() - for instance, s.typedArray().byteLengthLessThan(5) is the same as s.typedArray().lengthLessThan(5) but for the array's byte length.


Defining schemas (objects)

Back to top

// Properties are required by default:
const animal = s.object({
  name: s.string(),
  age: s.number()
});

Utility types for TypeScript

Back to top

For object validation Shapeshift exports 2 utility types that can be used to extract interfaces from schemas and define the structure of a schema as an interface beforehand respectively.

Extracting an interface from a schema

Back to top

You can use the InferType type to extract the interface from a schema, for example:

import { InferType, s } from '@sapphire/shapeshift';

const schema = s.object({
  foo: s.string(),
  bar: s.number(),
  baz: s.boolean(),
  qux: s.bigint(),
  quux: s.date()
});

type Inferredtype = InferType<typeof schema>;

// Expected type:
type Inferredtype = {
  foo: string;
  bar: number;
  baz: boolean;
  qux: bigint;
  quux: Date;
};
Defining the structure of a schema through an interface

Back to top

You can use the SchemaOf type to define the structure of a schema before defining the actual schema, for example:

import { s, SchemaOf } from '@sapphire/shapeshift';

interface IIngredient {
  ingredientId: string | undefined;
  name: string | undefined;
}

interface IInstruction {
  instructionId: string | undefined;
  message: string | undefined;
}

interface IRecipe {
  recipeId: string | undefined;
  title: string;
  description: string;
  instructions: IInstruction[];
  ingredients: IIngredient[];
}

type InstructionSchemaType = SchemaOf<IInstruction>;
// Expected Type: ObjectValidator<IInstruction>

type IngredientSchemaType = SchemaOf<IIngredient>;
// Expected Type: ObjectValidator<IIngredient>

type RecipeSchemaType = SchemaOf<IRecipe>;
// Expected Type: ObjectValidator<IRecipe>

const instructionSchema: InstructionSchemaType = s.object({
  instructionId: s.string().optional(),
  message: s.string()
});

const ingredientSchema: IngredientSchemaType = s.object({
  ingredientId: s.string().optional(),
  name: s.string()
});

const recipeSchema: RecipeSchemaType = s.object({
  recipeId: s.string().optional(),
  title: s.string(),
  description: s.string(),
  instructions: s.array(instructionSchema),
  ingredients: s.array(ingredientSchema)
});

.extend:

Back to top

You can add additional fields using either an object or an ObjectValidator, in this case, you will get a new object validator with the merged properties:

const animal = s.object({
  name: s.string().optional(),
  age: s.number()
});

const pet = animal.extend({
  owner: s.string().nullish()
});

const pet = animal.extend(
  s.object({
    owner: s.string().nullish()
  })
);

If both schemas share keys, an error will be thrown. Please use .omit on the first object if you desire this behaviour.

.pick / .omit:

Back to top

Inspired by TypeScript's built-in Pick and Omit utility types, all object schemas have the aforementioned methods that return a modifier version:

const pkg = s.object({
  name: s.string(),
  description: s.string(),
  dependencies: s.string().array()
});

const justTheName = pkg.pick(['name']);
// s.object({ name: s.string() });

const noDependencies = pkg.omit(['dependencies']);
// s.object({ name: s.string(), description: s.string() });

.partial

Back to top

Inspired by TypeScript's built-in Partial utility type, all object schemas have the aforementioned method that makes all properties optional:

const user = s.object({
  username: s.string(),
  password: s.string()
}).partial;

Which is the same as doing:

const user = s.object({
  username: s.string().optional(),
  password: s.string().optional()
});

.required

Back to top

Inspired by TypeScript's built-in Required utility type, all object schemas have the aforementioned method that makes all properties required:

const user = s.object({
  username: s.string().optional(),
  password: s.string().optional()
}).required;

Which is the same as doing:

const user = s.object({
  username: s.string(),
  password: s.string()
});

Handling unrecognized keys

Back to top

By default, Shapeshift will not include keys that are not defined by the schema during parsing:

const person = s.object({
  framework: s.string()
});

person.parse({
  framework: 'Sapphire',
  awesome: true
});
// => { name: 'Sapphire' }

.strict

Back to top

You can disallow unknown keys with .strict. If the input includes any unknown keys, an error will be thrown.

const person = s.object({
  framework: s.string()
}).strict;

person.parse({
  framework: 'Sapphire',
  awesome: true
});
// => throws ValidationError

.ignore

Back to top

You can use the .ignore getter to reset an object schema to the default behaviour (ignoring unrecognized keys).

.passthrough

Back to top

You can use the .passthrough getter to make the validator add the unrecognized properties the shape does not have, from the input.


BaseValidator: methods and properties

Back to top

All validations in Shapeshift contain certain methods.

  • .run(data: unknown): Result<T, Error>: given a validation, you can call this method to check whether or not the

    input is valid. If it is, a Result with success: true and a deep-cloned value will be returned with the given constraints and transformations. Otherwise, a Result with success: false and an error is returned.

  • .parse(data: unknown): T: given a validations, you can call this method to check whether or not the input is valid.

    If it is, a deep-cloned value will be returned with the given constraints and transformations. Otherwise, an error is thrown.

  • .transform<R>((value: T) => R): NopValidator<R>: adds a constraint that modifies the input:

import { s } from '@sapphire/shapeshift';

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

⚠️ .transform's functions must not throw. If a validation error is desired to be thrown, .reshape instead.

  • .reshape<R>((value: T) => Result<R, Error> | IConstraint): NopValidator<R>: adds a constraint able to both validate

    and modify the input:
import { s, Result } from '@sapphire/shapeshift';

const getLength = s.string().reshape((value) => Result.ok(value.length));
getLength.parse('Hello There'); // => 11

⚠️ .reshape's functions must not throw. If a validation error is desired to be thrown, use Result.err(error) instead.

  • .default(value: T | (() => T)): transform undefined into the given value or the callback's returned value:

const name = s.string().default('Sapphire');
name.parse('Hello'); // => 'Hello'
name.parse(undefined); // => 'Sapphire'
const number = s.number().default(Math.random);
number.parse(12); // => 12
number.parse(undefined); // => 0.989911985608602
number.parse(undefined); // => 0.3224350185068794

⚠️ The default values are not validated.

  • .optional: a convenience method that returns a union of the type with s.undefined().

s.string().optional(); // s.union(s.string(), s.undefined())
  • .nullable: a convenience method that returns a union of the type with s.nullable().

s.string().nullable(); // s.union(s.string(), s.nullable())
  • .nullish: a convenience method that returns a union of the type with s.nullish().

s.string().nullish(); // s.union(s.string(), s.nullish())
  • .array: a convenience method that returns an ArrayValidator with the type.

s.string().array(); // s.array(s.string())
  • .or: a convenience method that returns an UnionValidator with the type. This method is also overridden in

    UnionValidator to just append one more entry.
s.string().or(s.number());
// => s.union(s.string(), s.number())

s.object({ name: s.string() }).or(s.string(), s.number());
// => s.union(s.object({ name: s.string() }), s.string(), s.number())
  • .when: Adjust the schema based on a sibling or sinbling children fields.

For using when you provide an object literal where the key is is undefined, a value, or a matcher function; then provides the schema when is resolves truthy, and otherwise provides the schema when is resolves falsey.

Available options for providing is

When is is not provided (=== undefined) it is strictly resolved as Boolean(value) wherein value is the current value of the referenced sibling. Note that if multiple siblings are referenced then all the values of the array need to resolve truthy for the is to resolve truthy.

When is is a primitive literal it is strictly compared (===) to the current value.

If you want to use a different form of equality you can provide a function like: is: (value) => value === true.

Resolving of the key (first) parameter

For resolving the key parameter to its respective value we use lodash/get. This means that every way that Lodash supports resolving a key to its respective value is also supported by Shapeshift. This includes:

  • Simply providing a string or number like 'name' or 1.
  • Providing a string or number with a dot notation like 'name.first' (representative of a nested object structure of { 'name': { 'first': 'Sapphire' } } => resolves to Sapphire).
  • Providing a string or number with a bracket notation like 'name[0]' (representative of an array structure of { 'name': ['Sapphire', 'Framework'] } => resolves to Sapphire).
  • Providing a string or number with a dot and bracket notation like 'name[1].first' (representative of a nested object structure of { 'name': [{ 'first': 'Sapphire' }, { 'first': 'Framework' }] } => resolves to Framework).
Examples

Let's start with a basic example:

const whenPredicate = s.object({
  booleanLike: s.boolean(),
  numberLike: s.number().when('booleanLike', {
    then: (schema) => schema.greaterThanOrEqual(5),
    otherwise: (schema) => schema.lessThanOrEqual(5)
  })
});

whenPredicate.parse({ booleanLike: true, numberLike: 6 });
// => { booleanLike: true, numberLike: 6 }

whenPredicate.parse({ booleanLike: true, numberLike: 4 });
// => ExpectedConstraintError('s.number().greaterThanOrEqual', 'Invalid number value', 4, 'expected >= 5')

whenPredicate.parse({ booleanLike: false, numberLike: 4 });
// => { booleanLike: false, numberLike: 4 }

The provided key can also be an array of sibling children:

const whenPredicate = s.object({
  booleanLike: s.boolean(),
  stringLike: s.string(),
  numberLike: s.number().when(['booleanLike', 'stringLike'], {
	is: ([booleanLikeValue, stringLikeValue]) => booleanLikeValue === true && stringLikeValue === 'foobar',
    then: (schema) => schema.greaterThanOrEqual(5),
    otherwise: (schema) => schema.lessThanOrEqual(5)
  })
});

whenPredicate.parse({ booleanLike: true, stringLike: 'foobar', numberLike: 6 });
// => { booleanLike: true, numberLike: 6 }

whenPredicate.parse({ booleanLike: true, stringLike: 'barfoo', numberLike: 4 });
// => ExpectedConstraintError('s.number().greaterThanOrEqual', 'Invalid number value', 4, 'expected >= 5')

whenPredicate.parse({ booleanLike: false, stringLike: 'foobar' numberLike: 4 });
// => ExpectedConstraintError('s.number().greaterThanOrEqual', 'Invalid number value', 4, 'expected >= 5')

Enabling and disabling validation

Back to top

At times, you might want to have a consistent code base with validation, but would like to keep validation to the strict necessities instead of the in-depth constraints available in shapeshift. By calling setGlobalValidationEnabled you can disable validation at a global level, and by calling setValidationEnabled you can disable validation on a per-validator level.

When setting the validation enabled status per-validator, you can also set it to null to use the global setting.

import { setGlobalValidationEnabled } from '@sapphire/shapeshift';

setGlobalValidationEnabled(false);
import { s } from '@sapphire/shapeshift';

const predicate = s.string().lengthGreaterThan(5).setValidationEnabled(false);

Buy us some doughnuts

Back to top

Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!

We accept donations through Open Collective, Ko-fi, Paypal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.

Donate With Address
Open Collective Click Here
Ko-fi Click Here
Patreon Click Here
PayPal Click Here

Contributors

Back to top

Please make sure to read the Contributing Guide before making a pull request.

Thank you to all the people who already contributed to Sapphire!

shapeshift's People

Contributors

allcontributors[bot] avatar andarist avatar favna avatar igerman00 avatar imranbarbhuiya avatar khafradev avatar khasms avatar kyranet avatar legendhimself avatar realshadownova avatar renovate-bot avatar renovate[bot] avatar vladfrangu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

shapeshift's Issues

request: Implement something like zod's nativeEnum

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

Implement something like zod's nativeEnum, so we can pass in a TS enum and validate using it (probably with handling transformation of string key -> numeric value too for numeric enums)

Desired solution

See above

Alternatives considered

N.A.

Additional context

Originally posted by @vladfrangu in #44 (comment)

request: Add label support to validators, constraints, etc

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

One of the biggest issues with shapeshift right now is that when running a validator of the form s.string.parse(input), the error received is not really useful (it just says that the validator failed, not why or where or for what).

Other modules (notably ow uses callsites to infer the label from the callsite) either allow this or use some interesting mechanisms to detect/infer the label. Since we aim for speed ⚡ here, we should probably stick to letting users provide a label (or even a label factory?)

Desired solution

Support for adding labels on validators (mayhaps s.string.label('url') instead of s.string('label') as the latter is breaking which is not ideal).

Would be nice to also have something like s.string.label((input) => translate('bad-input', input)) to allow dynamic labels in one predicate (for instance for the 1 person planning on using shapeshift in an i18n context)

Alternatives considered

Not supporting this, which is a downside...so none :D

Additional context

No response

request: Narrowing types from schema

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

Add some method of parsing a schema that narrows the type down and doesn't throw an error

Desired solution

import { s } from '@sapphire/shapeshift'

const schema = s.object({
  a: s.string,
  b: s.number
})

// a value from an api, for example
const toCompare: any = {
  a: 'Hello',
  b: 1
}

if (!schema.is(toCompare)) {
  return 'some error message'
}

toCompare // InferType<typeof schema>

Alternatives considered

import { s } from '@sapphire/shapeshift'

const schema = s.object({
  a: s.string,
  b: s.number
})

const obj: any = {
  a: 'hello',
  b: 1
}

const result = schema.run(obj)

// Note: checking result.isErr() does NOT
// limit the type correctly :(
if (!result.isOk()) {
  return 'some error message'
}

result.value // InferType<typeof schema>

or closer to what I'd prefer built in

const isSchema = <T>(obj: unknown): obj is T =>schema.run(obj).isOk()

if (!isSchema<InferType<typeof schema>>(obj)) {
  return 'some error message'
}

obj // InferType<typeof schema>

Additional context

Thanks

request: cloudflare worker support

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

From #248 I could gather that cloudflare worker support is something that was being kept in mind. I recently tried to use @discordjs/builders which depends on sapphire and it gave me errors for needing to enable node_compat, something I could gather is heavily discouraged

Desired solution

Implement an alternative for the inspect function imports from util

Alternatives considered

Find another package which provides the same functionality as the inspect function

Additional context

No response

bug: Type 'T' does not satisfy the constraint 'object'

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

On build it throws an error
image

Steps To Reproduce

not sure how to reproduce, after updating to latest version I run into this error, the package is not installed as a dependency on my project but seems being used by library I used which I'm not sure which one

Expected behavior

should not throw an error

Screenshots

No response

Additional context

No response

bug: Reshape is in the docs, but is nowhere in the code

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

When trying to use s.string.reshape(), TypeScript complains about that method not existing. A simple recursive grep shows that the method is only in README.md, but it nowhere in the code.

Steps To Reproduce

  1. grep reshape -r ./node_modules/@sapphire/shapeshift/dist/ (or rg reshape ./node_modules/@sapphire/shapeshift/dist/)
  2. Command finds nothing

Expected behavior

The method exists

Screenshots

Screenshot

Additional context

No response

request: `setValidationEnabled` should [un]wrap the validator into an `PartialValidator<T>`

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

Right now, all validators have checks for whether or not they should run validations, as seen below:

// If validation is disabled (at the validator or global level), we only run the `handle` method, which will do some basic checks
// (like that the input is a string for a string validator)
if (!this.shouldRunConstraints) {
return this.handle(value).unwrap() as R;
}

This comes with a large performance impact, specially from those who desire to use the library without conditional validation. Also goes against Shapeshift's internal design of running the least amount of conditionals as possible.

Before we added conditional validation, Shapeshift was comfortably among the fastest libraries in our benchmarks.

Desired solution

A wrapper would solve the performance impact by making the validators always run the logic and constraints, where the PartialValidator<T> would exclusively only run the handler and never the constraints (with no extra checks, of course).

For function (dynamic validation), we can also add a second class, or add a check in PartialValidator<T>, invalidating the last sentence in the previous paragraph.

Unwrapping a PartialValidator<T> should give back the underlying, fully-checked validator.

Alternatives considered

N/a.

Additional context

No response

bug: shapeshift 3.5.0 imports fast-deep-equal wrong breaking it

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

when you use type module in package.json
shapeshift imports fast-deep-equal as
import fastDeepEqual from "fast-deep-equal/es6";
breaking it
changing the code to
import fastDeepEqual from "fast-deep-equal";
fixes it.

Steps To Reproduce

set the type to module

Expected behavior

it shouldnt crash

Screenshots

image

Additional context

No response

bug: Object validators considers keys that have a default return value as required

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

Using an object validator with a key that has a value with a default validator attached to it makes it be considered required

Steps To Reproduce

const obj = s.object({
	limit: s.number.default(100)
});

Expected behavior

It works

Screenshots

No response

Additional context

CombinedPropertyError: Received one or more errors
    at ObjectValidator.handleIgnoreStrategy (/Users/vlad/Development/Apify/apify-ts/node_modules/@sapphire/shapeshift/dist/index.js:1105:72)
    at ObjectValidator.handleStrategy (/Users/vlad/Development/Apify/apify-ts/node_modules/@sapphire/shapeshift/dist/index.js:1004:47)
    at ObjectValidator.handle (/Users/vlad/Development/Apify/apify-ts/node_modules/@sapphire/shapeshift/dist/index.js:1079:17)
    at ObjectValidator.parse (/Users/vlad/Development/Apify/apify-ts/node_modules/@sapphire/shapeshift/dist/index.js:113:88)
    at RequestQueueClient.listHead (/Users/vlad/Development/Apify/apify-ts/packages/memory-storage/dist/resource-clients/request-queue.js:135:12)
    at file:///Users/vlad/Development/Apify/apify-ts/test/e2e/globs-vs-regexes-vs-pseudoUrls/test.mjs:90:52 {
  errors: [
    [
      'limit',
      MissingPropertyError: A required property is missing
          at ObjectValidator.handleIgnoreStrategy (/Users/vlad/Development/Apify/apify-ts/node_modules/@sapphire/shapeshift/dist/index.js:1101:27)
          at ObjectValidator.handleStrategy (/Users/vlad/Development/Apify/apify-ts/node_modules/@sapphire/shapeshift/dist/index.js:1004:47)
          at ObjectValidator.handle (/Users/vlad/Development/Apify/apify-ts/node_modules/@sapphire/shapeshift/dist/index.js:1079:17)
          at ObjectValidator.parse (/Users/vlad/Development/Apify/apify-ts/node_modules/@sapphire/shapeshift/dist/index.js:113:88)
          at RequestQueueClient.listHead (/Users/vlad/Development/Apify/apify-ts/packages/memory-storage/dist/resource-clients/request-queue.js:135:12)
          at file:///Users/vlad/Development/Apify/apify-ts/test/e2e/globs-vs-regexes-vs-pseudoUrls/test.mjs:90:52 {
        property: 'limit'
      }
    ]
  ]
}

bug: Types not working with typescript 4.8

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

With the new changes to the {} type, sapphire is throwing errors when compiling.

Steps To Reproduce

  1. Update to typescript 4.8
  2. Run tsc

Expected behavior

Not throwing an error.

Screenshots

unknown-38

Additional context

No response

bug: UnionValidator#setValidationEnabled does not make its members respect that

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

Found when disabling validation in @discordjs/builders on EmbedBuilder#setDescription() it would still throw a CombinedError with ExpectedValidationError in it.

Steps To Reproduce

import {s} from "@sapphire/shapeshift";

s.string
.lengthGreaterThanOrEqual(10)
.nullish
.setValidationEnabled(false).parse("test");

Expected behavior

It should not throw any Error.

Screenshots

No response

Additional context

No response

bug: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import

Is there an existing issue for this?

  • I have searched the existing issues (The #155 is about it too)

Description of the bug

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'c:\Users\jpedr\Desktop\Visual Code\Node.js\Botter V4.1\node_modules\fast-deep-equal\es6' is not supported resolving ES modules imported from c:\Users\jpedr\Desktop\Visual Code\Node.js\Botter V4.1\node_modules\@sapphire\shapeshift\dist\index.mjs
Did you mean to import fast-deep-equal/es6/index.js?
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:390:17)
    at moduleResolve (node:internal/modules/esm/resolve:915:10)
    at defaultResolve (node:internal/modules/esm/resolve:1005:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
    at link (node:internal/modules/esm/module_job:78:36) {
  code: 'ERR_UNSUPPORTED_DIR_IMPORT',
  url: 'file:///c:/Users/jpedr/Desktop/Visual%20Code/Node.js/Botter%20V4.1/node_modules/fast-deep-equal/es6'
}
    at consoleBoth (file:///c:/Users/jpedr/Desktop/Visual%20Code/Node.js/Botter%20V4.1/src/Functions/Errors/catchHandlers.js:78:10)
    at nullCatch (file:///c:/Users/jpedr/Desktop/Visual%20Code/Node.js/Botter%20V4.1/src/Functions/Errors/catchHandlers.js:3:2)
    at async FileLoader.load (file:///c:/Users/jpedr/Desktop/Visual%20Code/Node.js/Botter%20V4.1/src/Structures/Utils/FileLoader.js:71:26)
    at async loadCommands (file:///c:/Users/jpedr/Desktop/Visual%20Code/Node.js/Botter%20V4.1/src/Structures/Client/Client.js:190:15)
    at async extendClient (file:///c:/Users/jpedr/Desktop/Visual%20Code/Node.js/Botter%20V4.1/src/Structures/Client/Client.js:123:3)
    at async file:///c:/Users/jpedr/Desktop/Visual%20Code/Node.js/Botter%20V4.1/src/index.js:96:1

Steps To Reproduce

Using the tree:

[email protected]
  └─┬ @discordjs/[email protected]
       └── @sapphire/[email protected]

The error is throw

Expected behavior

The #155 should be fixed this issue, but the compiled index.mjs still throws the error

Screenshots

No response

Additional context

No response

bug: Type 'T' does not satisfy the constraint 'object'

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

https://stackoverflow.com/questions/73761953/sapphire-shapeshift-discordjs-type-t-does-not-satisfy-the-constraint-objec

Using DiscordJS and typescript, when I try to build using TSC, I get errors in sapphire/shapeshift

node_modules/@sapphire/shapeshift/dist/index.d.ts:590:44 - error TS2344: Type 'T' does not satisfy the constraint 'object'.

590 declare type SchemaOf<T> = ObjectValidator<T>;
                                               ~

  node_modules/@sapphire/shapeshift/dist/index.d.ts:590:23
    590 declare type SchemaOf<T> = ObjectValidator<T>;
                              ~
    This type parameter might need an `extends object` constraint.

node_modules/@sapphire/shapeshift/dist/index.d.ts:641:65 - error TS2344: Type 'T' does not satisfy the constraint 'object'.

641     object<T>(shape: MappedObjectValidator<T>): ObjectValidator<T, UndefinedToOptional<T>>;
                                                                    ~

  node_modules/@sapphire/shapeshift/dist/index.d.ts:641:12
    641     object<T>(shape: MappedObjectValidator<T>): ObjectValidator<T, UndefinedToOptional<T>>;
                   ~
    This type parameter might need an `extends object` constraint.


Found 2 errors in the same file, starting at: node_modules/@sapphire/shapeshift/dist/index.d.ts:590

Steps To Reproduce

  1. Setup a project using DiscordJS14 and typescript
  2. Build using TSC

Expected behavior

Builds properly

Screenshots

No response

Additional context

No response

bug: BaseError is not correctly exported

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

Node v16, TypeScript, CommonJS, shapeshift 2.1.0

import { BaseError } from '@sapphire/shapeshift';

console.log(BaseError); // logs undefined

So the question is why BaseError is undefined?

And if this is normal behavior, then how can I check that the error was thrown by shapeshift? The original idea was to use instanceof BaseError since all other errors inherits from it

Steps To Reproduce

  1. Environment: Node v16, TypeScript, CommonJS, shapeshift 2.1.0
  2. Import BaseError and log it into console

Expected behavior

BaseError should be exported correctly

OR

Tell me how to check that the error was thrown by shapeshift without instanceof BaseError

Screenshots

No response

Additional context

Sorry if this is not a bug. I sent a such message to the Sapphire discord server but no response :(

question: Multiple errors causing my discord bot to crash

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

I'm not quite sure what's really going on. Sapphire is being called as a dependency by the discord.js package. This has been happening often and I'm not sure what I can do about it. I've tried getting support from the discord.js folks and they haven't been helpful. You can review the errors here.

Steps To Reproduce

I don't have any steps. I only know that when an interactionCreate event is fired in my discord bot, the above error occurs.

Expected behavior

No crashing? I'm not quite sure what sapphire is even for. Just know that it's a dependency for discord.js.

Screenshots

No response

Additional context

I'm still new to making discord bots using discord.js. And still pretty new to javascript in general. So I'm not really sure what the next steps in diagnosing this issue. But my bot is highly unstable due to this issue and I need to figure out how to mitigate the crashing.

request: cloudflare worker support

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

when bundling with cloudflare workers, I get an error:

    ../../node_modules/@sapphire/shapeshift/dist/index.mjs:2:24:
      2 │ import { inspect } from 'node:util';
        ╵                         ~~~~~~~~~~~

  The package "node:util" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, 
which will remove this error.


X [ERROR] Build failed with 1 error:

  ../../node_modules/@sapphire/shapeshift/dist/index.mjs:2:24: ERROR: Could not resolve "node:util"

Desired solution

all that's needed is to remove the node: prefix, then the node_compat flag can actually work. Yeah it's really annoying.

Alternatives considered

using another library I guess

Additional context

No response

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update dependency @commitlint/cli to ^19.4.0

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/auto-deprecate.yml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/ci-node-16.yml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/continuous-delivery.yml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/continuous-integration.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • codecov/codecov-action v4
.github/workflows/deprecate-on-merge.yml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/documentation.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/upload-artifact v4
  • actions/checkout v4
  • actions/setup-node v4
  • actions/download-artifact v4
  • actions/checkout v4
  • nick-fields/retry v3
.github/workflows/labelsync.yml
  • actions/checkout v4
  • crazy-max/ghaction-github-labeler v5
.github/workflows/publish.yml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/release-crosspost.yml
  • kludge-cs/gitcord-release-changelogger v3.0.0
npm
package.json
  • fast-deep-equal ^3.1.3
  • lodash ^4.17.21
  • @commitlint/cli ^19.3.0
  • @commitlint/config-conventional ^19.2.2
  • @favware/cliff-jumper ^4.0.3
  • @favware/npm-deprecate ^1.0.7
  • @sapphire/eslint-config ^5.0.5
  • @sapphire/prettier-config ^2.0.0
  • @sapphire/ts-config ^5.0.1
  • @types/jsdom ^21.1.7
  • @types/lodash ^4.17.7
  • @vitest/coverage-v8 ^2.0.5
  • cz-conventional-changelog ^3.3.0
  • esbuild-plugins-node-modules-polyfill ^1.6.4
  • eslint ^8.57.0
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-prettier ^5.2.1
  • jsdom ^24.1.1
  • lint-staged ^15.2.8
  • prettier ^3.3.3
  • rimraf ^6.0.1
  • tsup ^8.2.4
  • vitest ^2.0.5
  • ansi-regex ^5.0.1
  • minimist ^1.2.8
  • yarn 4.4.0

  • Check this box to trigger a request for Renovate to run again on this repository

request: refactor code in order to rely less on type casting

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

In a lot of cases we need to add type casts to our code because of the way it is set up. This makes development really annoying and it also removes a layer of type safety offered by TypeScript going forward. It is therefore prudent to remove the type casts and instead have TypeScript resolves types properly automatically.

Desired solution

Remove type casts such as shown at #40 (comment) and ensure code compiles successfully.

Alternatives considered

Continue to use type casts in the code.

Additional context

Review comment from @vladfrangu #40 (comment)

request: add `s.string.phone` to regex validate a phone number

Is there an existing issue or pull request for this?

  • I have searched the existing issues and pull requests

Feature description

We have s.string.email and in forms it is often required to enter both an email and a phone number so lets also add s.string.phone

Desired solution

  • Add a new validation s.string.phone
  • Add unit tests

Alternatives considered

Using s.string.regex with the regex for a phone number, however this is not ideal because then end-users have to constantly look up the phone regex online

Additional context

Refer to https://ihateregex.io/expr/phone/ for an example for a phone regex

bug: `BaseValidator` return type is invalid

Is there an existing issue for this?

  • I have searched the existing issues

Description of the bug

Originally discussed in the discord server. DefaultValidator's return type contains an extra undefined.

Steps To Reproduce

const predicate = s.string.default("Hello");

predicated.parse("hello");

// string | undefined

Expected behavior

It's return type should be string.

Screenshots

image

Additional context

Discord message link: https://discord.com/channels/737141877803057244/934614121687687179/947818834205741066

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.