Skip to content

Commit

Permalink
fix: expand method names (#100)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `date.eq` has been renamed to `date.equal`
BREAKING CHANGE: `string.lengthLt` has been renamed to `string.lengthLessThan`
BREAKING CHANGE: `string.lengthLe` has been renamed to `string.lengthLessThanOrEqual`
BREAKING CHANGE: `string.lengthGt` has been renamed to `string.lengthGreaterThan`
BREAKING CHANGE: `string.lengthGe` has been renamed to `string.lengthGreaterThanOrEqual`
BREAKING CHANGE: `string.lengthEq` has been renamed to `string.lengthEqual`
BREAKING CHANGE: `string.lengthNe` has been renamed to `string.lengthNotEqual`
BREAKING CHANGE: `number.gt` has been renamed to `number.greaterThan`
BREAKING CHANGE: `number.ge` has been renamed to `number.greaterThanOrEqual`
BREAKING CHANGE: `number.lt` has been renamed to `number.lessThan`
BREAKING CHANGE: `number.le` has been renamed to `number.lessThanOrEqual`
BREAKING CHANGE: `number.eq` has been renamed to `number.equal`
BREAKING CHANGE: `number.ne` has been renamed to `number.notEqual`
BREAKING CHANGE: `bigint.gt` has been renamed to `bigint.greaterThan`
BREAKING CHANGE: `bigint.ge` has been renamed to `bigint.greaterThanOrEqual`
BREAKING CHANGE: `bigint.lt` has been renamed to `bigint.lessThan`
BREAKING CHANGE: `bigint.le` has been renamed to `bigint.lessThanOrEqual`
BREAKING CHANGE: `bigint.eq` has been renamed to `bigint.equal`
BREAKING CHANGE: `bigint.ne` has been renamed to `bigint.notEqual`
BREAKING CHANGE: `boolean.eq` has been renamed to `boolean.equal`
BREAKING CHANGE: `boolean.ne` has been renamed to `boolean.notEqual`
BREAKING CHANGE: `array.lengthLt` has been renamed to `array.lengthLessThan`
BREAKING CHANGE: `array.lengthLe` has been renamed to `array.lengthLessThanOrEqual`
BREAKING CHANGE: `array.lengthGt` has been renamed to `array.lengthGreaterThan`
BREAKING CHANGE: `array.lengthGe` has been renamed to `array.lengthGreaterThanOrEqual`
BREAKING CHANGE: `array.lengthEq` has been renamed to `array.lengthEqual`
BREAKING CHANGE: `array.lengthNe` has been renamed to `array.lengthNotEqual`
BREAKING CHANGE: `typedArray.lengthLt` has been renamed to `typedArray.lengthLessThan`
BREAKING CHANGE: `typedArray.lengthLe` has been renamed to `typedArray.lengthLessThanOrEqual`
BREAKING CHANGE: `typedArray.lengthGt` has been renamed to `typedArray.lengthGreaterThan`
BREAKING CHANGE: `typedArray.lengthGe` has been renamed to `typedArray.lengthGreaterThanOrEqual`
BREAKING CHANGE: `typedArray.lengthEq` has been renamed to `typedArray.lengthEqual`
BREAKING CHANGE: `typedArray.lengthNe` has been renamed to `typedArray.lengthNotEqual`
BREAKING CHANGE: `typedArray.byteLengthLt` has been renamed to `typedArray.byteLengthLessThan`
BREAKING CHANGE: `typedArray.byteLengthLe` has been renamed to `typedArray.byteLengthLessThanOrEqual`
BREAKING CHANGE: `typedArray.byteLengthGt` has been renamed to `typedArray.byteLengthGreaterThan`
BREAKING CHANGE: `typedArray.byteLengthGe` has been renamed to `typedArray.byteLengthGreaterThanOrEqual`
BREAKING CHANGE: `typedArray.byteLengthEq` has been renamed to `typedArray.byteLengthEqual`
BREAKING CHANGE: `typedArray.byteLengthNe` has been renamed to `typedArray.byteLengthNotEqual`
  • Loading branch information
vladfrangu authored May 4, 2022
1 parent c83b3d0 commit 741490f
Show file tree
Hide file tree
Showing 27 changed files with 640 additions and 549 deletions.
86 changes: 43 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ s.literal('sapphire');
s.literal(12);
s.literal(420n);
s.literal(true);
s.literal(new Date(1639278160000)); // s.date.eq(1639278160000);
s.literal(new Date(1639278160000)); // s.date.equal(1639278160000);
```

#### Strings

ShapeShift includes a handful of string-specific validations:

```typescript
s.string.lengthLt(5);
s.string.lengthLe(5);
s.string.lengthGt(5);
s.string.lengthGe(5);
s.string.lengthEq(5);
s.string.lengthNe(5);
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();
Expand All @@ -119,22 +119,22 @@ s.string.ipv6;
ShapeShift includes a handful of number-specific validations:

```typescript
s.number.gt(5); // > 5
s.number.ge(5); // >= 5
s.number.lt(5); // < 5
s.number.le(5); // <= 5
s.number.eq(5); // === 5
s.number.ne(5); // !== 5
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.eq(NaN); // special case: Number.isNaN
s.number.ne(NaN); // special case: !Number.isNaN
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; // .ge(0)
s.number.negative; // .lt(0)
s.number.positive; // .greaterThanOrEqual(0)
s.number.negative; // .lessThan(0)

s.number.divisibleBy(5); // Divisible by 5
```
Expand All @@ -157,15 +157,15 @@ s.number.ceil; // Transforms the number to the result of `Math.ceil`
ShapeShift includes a handful of number-specific validations:

```typescript
s.bigint.gt(5n); // > 5n
s.bigint.ge(5n); // >= 5n
s.bigint.lt(5n); // < 5n
s.bigint.le(5n); // <= 5n
s.bigint.eq(5n); // === 5n
s.bigint.ne(5n); // !== 5n
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; // .ge(0n)
s.bigint.negative; // .lt(0n)
s.bigint.positive; // .greaterThanOrEqual(0n)
s.bigint.negative; // .lessThan(0n)

s.bigint.divisibleBy(5n); // Divisible by 5n
```
Expand All @@ -187,11 +187,11 @@ ShapeShift includes a few boolean-specific validations:
s.boolean.true; // value must be true
s.boolean.false; // value must be false

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

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

#### Arrays
Expand All @@ -204,18 +204,18 @@ const stringArray = s.string.array;
ShapeShift includes a handful of array-specific validations:

```typescript
s.string.array.lengthLt(5); // Must have less than 5 elements
s.string.array.lengthLe(5); // Must have 5 or less elements
s.string.array.lengthGt(5); // Must have more than 5 elements
s.string.array.lengthGe(5); // Must have 5 or more elements
s.string.array.lengthEq(5); // Must have exactly 5 elements
s.string.array.lengthNe(5); // Must not have exactly 5 elements
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))
```

> **Note**: All `.length` methods define tuple types with the given amount of elements. For example, `s.string.array.lengthGe(2)`'s inferred type is `[string, string, ...string[]]`
> **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

Expand Down Expand Up @@ -441,18 +441,18 @@ const bigUint64Array = s.bigUint64Array;
ShapeShift includes a handful of validations specific to typed arrays.

```typescript
s.typedArray().lengthLt(5); // Length must be less than 5
s.typedArray().lengthLe(5); // Length must be 5 or less
s.typedArray().lengthGt(5); // Length must be more than 5
s.typedArray().lengthGe(5); // Length must be 5 or more
s.typedArray().lengthEq(5); // Length must be exactly 5
s.typedArray().lengthNe(5); // Length must not be 5
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().byteLengthLt(5)` is the same as `s.typedArray().lengthLt(5)` but for the array's byte length.
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.

### BaseValidator: methods and properties

Expand Down
37 changes: 23 additions & 14 deletions src/constraints/ArrayLengthConstraints.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { ExpectedConstraintError } from '../lib/errors/ExpectedConstraintError';
import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators';
import { Comparator, equal, greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual, notEqual } from './util/operators';

export type ArrayConstraintName = `s.array(T).length${'Lt' | 'Le' | 'Gt' | 'Ge' | 'Eq' | 'Ne' | 'Range' | 'RangeInclusive' | 'RangeExclusive'}`;
export type ArrayConstraintName = `s.array(T).length${
| 'LessThan'
| 'LessThanOrEqual'
| 'GreaterThan'
| 'GreaterThanOrEqual'
| 'Equal'
| 'NotEqual'
| 'Range'
| 'RangeInclusive'
| 'RangeExclusive'}`;

function arrayLengthComparator<T>(comparator: Comparator, name: ArrayConstraintName, expected: string, length: number): IConstraint<T[]> {
return {
Expand All @@ -15,34 +24,34 @@ function arrayLengthComparator<T>(comparator: Comparator, name: ArrayConstraintN
};
}

export function arrayLengthLt<T>(value: number): IConstraint<T[]> {
export function arrayLengthLessThan<T>(value: number): IConstraint<T[]> {
const expected = `expected.length < ${value}`;
return arrayLengthComparator(lt, 's.array(T).lengthLt', expected, value);
return arrayLengthComparator(lessThan, 's.array(T).lengthLessThan', expected, value);
}

export function arrayLengthLe<T>(value: number): IConstraint<T[]> {
export function arrayLengthLessThanOrEqual<T>(value: number): IConstraint<T[]> {
const expected = `expected.length <= ${value}`;
return arrayLengthComparator(le, 's.array(T).lengthLe', expected, value);
return arrayLengthComparator(lessThanOrEqual, 's.array(T).lengthLessThanOrEqual', expected, value);
}

export function arrayLengthGt<T>(value: number): IConstraint<T[]> {
export function arrayLengthGreaterThan<T>(value: number): IConstraint<T[]> {
const expected = `expected.length > ${value}`;
return arrayLengthComparator(gt, 's.array(T).lengthGt', expected, value);
return arrayLengthComparator(greaterThan, 's.array(T).lengthGreaterThan', expected, value);
}

export function arrayLengthGe<T>(value: number): IConstraint<T[]> {
export function arrayLengthGreaterThanOrEqual<T>(value: number): IConstraint<T[]> {
const expected = `expected.length >= ${value}`;
return arrayLengthComparator(ge, 's.array(T).lengthGe', expected, value);
return arrayLengthComparator(greaterThanOrEqual, 's.array(T).lengthGreaterThanOrEqual', expected, value);
}

export function arrayLengthEq<T>(value: number): IConstraint<T[]> {
export function arrayLengthEqual<T>(value: number): IConstraint<T[]> {
const expected = `expected.length === ${value}`;
return arrayLengthComparator(eq, 's.array(T).lengthEq', expected, value);
return arrayLengthComparator(equal, 's.array(T).lengthEqual', expected, value);
}

export function arrayLengthNe<T>(value: number): IConstraint<T[]> {
export function arrayLengthNotEqual<T>(value: number): IConstraint<T[]> {
const expected = `expected.length !== ${value}`;
return arrayLengthComparator(ne, 's.array(T).lengthNe', expected, value);
return arrayLengthComparator(notEqual, 's.array(T).lengthNotEqual', expected, value);
}

export function arrayLengthRange<T>(start: number, endBefore: number): IConstraint<T[]> {
Expand Down
35 changes: 21 additions & 14 deletions src/constraints/BigIntConstraints.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { ExpectedConstraintError } from '../lib/errors/ExpectedConstraintError';
import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators';
import { Comparator, equal, greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual, notEqual } from './util/operators';

export type BigIntConstraintName = `s.bigint.${'lt' | 'le' | 'gt' | 'ge' | 'eq' | 'ne' | 'divisibleBy'}`;
export type BigIntConstraintName = `s.bigint.${
| 'lessThan'
| 'lessThanOrEqual'
| 'greaterThan'
| 'greaterThanOrEqual'
| 'equal'
| 'notEqual'
| 'divisibleBy'}`;

function bigintComparator(comparator: Comparator, name: BigIntConstraintName, expected: string, number: bigint): IConstraint<bigint> {
return {
Expand All @@ -15,34 +22,34 @@ function bigintComparator(comparator: Comparator, name: BigIntConstraintName, ex
};
}

export function bigintLt(value: bigint): IConstraint<bigint> {
export function bigintLessThan(value: bigint): IConstraint<bigint> {
const expected = `expected < ${value}n`;
return bigintComparator(lt, 's.bigint.lt', expected, value);
return bigintComparator(lessThan, 's.bigint.lessThan', expected, value);
}

export function bigintLe(value: bigint): IConstraint<bigint> {
export function bigintLessThanOrEqual(value: bigint): IConstraint<bigint> {
const expected = `expected <= ${value}n`;
return bigintComparator(le, 's.bigint.le', expected, value);
return bigintComparator(lessThanOrEqual, 's.bigint.lessThanOrEqual', expected, value);
}

export function bigintGt(value: bigint): IConstraint<bigint> {
export function bigintGreaterThan(value: bigint): IConstraint<bigint> {
const expected = `expected > ${value}n`;
return bigintComparator(gt, 's.bigint.gt', expected, value);
return bigintComparator(greaterThan, 's.bigint.greaterThan', expected, value);
}

export function bigintGe(value: bigint): IConstraint<bigint> {
export function bigintGreaterThanOrEqual(value: bigint): IConstraint<bigint> {
const expected = `expected >= ${value}n`;
return bigintComparator(ge, 's.bigint.ge', expected, value);
return bigintComparator(greaterThanOrEqual, 's.bigint.greaterThanOrEqual', expected, value);
}

export function bigintEq(value: bigint): IConstraint<bigint> {
export function bigintEqual(value: bigint): IConstraint<bigint> {
const expected = `expected === ${value}n`;
return bigintComparator(eq, 's.bigint.eq', expected, value);
return bigintComparator(equal, 's.bigint.equal', expected, value);
}

export function bigintNe(value: bigint): IConstraint<bigint> {
export function bigintNotEqual(value: bigint): IConstraint<bigint> {
const expected = `expected !== ${value}n`;
return bigintComparator(ne, 's.bigint.ne', expected, value);
return bigintComparator(notEqual, 's.bigint.notEqual', expected, value);
}

export function bigintDivisibleBy(divider: bigint): IConstraint<bigint> {
Expand Down
36 changes: 22 additions & 14 deletions src/constraints/DateConstraints.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { ExpectedConstraintError } from '../lib/errors/ExpectedConstraintError';
import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators';
import { Comparator, equal, greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual, notEqual } from './util/operators';

export type DateConstraintName = `s.date.${'lt' | 'le' | 'gt' | 'ge' | 'eq' | 'ne' | 'valid' | 'invalid'}`;
export type DateConstraintName = `s.date.${
| 'lessThan'
| 'lessThanOrEqual'
| 'greaterThan'
| 'greaterThanOrEqual'
| 'equal'
| 'notEqual'
| 'valid'
| 'invalid'}`;

function dateComparator(comparator: Comparator, name: DateConstraintName, expected: string, number: number): IConstraint<Date> {
return {
Expand All @@ -15,34 +23,34 @@ function dateComparator(comparator: Comparator, name: DateConstraintName, expect
};
}

export function dateLt(value: Date): IConstraint<Date> {
export function dateLessThan(value: Date): IConstraint<Date> {
const expected = `expected < ${value.toISOString()}`;
return dateComparator(lt, 's.date.lt', expected, value.getTime());
return dateComparator(lessThan, 's.date.lessThan', expected, value.getTime());
}

export function dateLe(value: Date): IConstraint<Date> {
export function dateLessThanOrEqual(value: Date): IConstraint<Date> {
const expected = `expected <= ${value.toISOString()}`;
return dateComparator(le, 's.date.le', expected, value.getTime());
return dateComparator(lessThanOrEqual, 's.date.lessThanOrEqual', expected, value.getTime());
}

export function dateGt(value: Date): IConstraint<Date> {
export function dateGreaterThan(value: Date): IConstraint<Date> {
const expected = `expected > ${value.toISOString()}`;
return dateComparator(gt, 's.date.gt', expected, value.getTime());
return dateComparator(greaterThan, 's.date.greaterThan', expected, value.getTime());
}

export function dateGe(value: Date): IConstraint<Date> {
export function dateGreaterThanOrEqual(value: Date): IConstraint<Date> {
const expected = `expected >= ${value.toISOString()}`;
return dateComparator(ge, 's.date.ge', expected, value.getTime());
return dateComparator(greaterThanOrEqual, 's.date.greaterThanOrEqual', expected, value.getTime());
}

export function dateEq(value: Date): IConstraint<Date> {
export function dateEqual(value: Date): IConstraint<Date> {
const expected = `expected === ${value.toISOString()}`;
return dateComparator(eq, 's.date.eq', expected, value.getTime());
return dateComparator(equal, 's.date.equal', expected, value.getTime());
}

export function dateNe(value: Date): IConstraint<Date> {
export function dateNotEqual(value: Date): IConstraint<Date> {
const expected = `expected !== ${value.toISOString()}`;
return dateComparator(ne, 's.date.ne', expected, value.getTime());
return dateComparator(notEqual, 's.date.notEqual', expected, value.getTime());
}

export const dateInvalid: IConstraint<Date> = {
Expand Down
Loading

0 comments on commit 741490f

Please sign in to comment.