Skip to content

Commit

Permalink
chore: never gonna tell a lie and hurt you
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Feb 3, 2022
1 parent 9cd5dd1 commit 41bebea
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators';

function arrayLengthComparator<T>(comparator: Comparator, name: string, expected: string, length: number): IConstraint<T[]> {
export type ArrayLengthComparatorConstraintName = `s.array(T).length${'Lt' | 'Le' | 'Gt' | 'Ge' | 'Eq' | 'Ne'}`;
export type ArrayConstraintName = ArrayLengthComparatorConstraintName;

function arrayLengthComparator<T>(
comparator: Comparator,
name: ArrayLengthComparatorConstraintName,
expected: string,
length: number
): IConstraint<T[]> {
return {
run(input: T[]) {
return comparator(input.length, length) //
Expand Down
5 changes: 4 additions & 1 deletion src/constraints/BigIntConstraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators';

function bigintComparator(comparator: Comparator, name: string, expected: string, number: bigint): IConstraint<bigint> {
export type BigIntComparatorConstraintName = `s.bigint.${'lt' | 'le' | 'gt' | 'ge' | 'eq' | 'ne'}`;
export type BigIntConstraintName = BigIntComparatorConstraintName;

function bigintComparator(comparator: Comparator, name: BigIntComparatorConstraintName, expected: string, number: bigint): IConstraint<bigint> {
return {
run(input: bigint) {
return comparator(input, number) //
Expand Down
2 changes: 2 additions & 0 deletions src/constraints/BooleanConstraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ConstraintError } from '../lib/errors/ConstraintError';
import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';

export type BooleanConstraintName = `s.boolean.${boolean}`;

export const booleanTrue: IConstraint<boolean, true> = {
run(input: boolean) {
return input //
Expand Down
5 changes: 4 additions & 1 deletion src/constraints/DateConstraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators';

function dateComparator(comparator: Comparator, name: string, expected: string, number: number): IConstraint<Date> {
export type DateComparatorConstraintName = `s.date.${'lt' | 'le' | 'gt' | 'ge' | 'eq' | 'ne'}`;
export type DateConstraintName = DateComparatorConstraintName | `s.date.${'eq(NaN)' | 'ne(NaN)'}`;

function dateComparator(comparator: Comparator, name: DateComparatorConstraintName, expected: string, number: number): IConstraint<Date> {
return {
run(input: Date) {
return comparator(input.getTime(), number) //
Expand Down
7 changes: 6 additions & 1 deletion src/constraints/NumberConstraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators';

function numberComparator(comparator: Comparator, name: string, expected: string, number: number): IConstraint<number> {
export type NumberComparatorConstraintName = `s.number.${'lt' | 'le' | 'gt' | 'ge' | 'eq' | 'ne'}`;
export type NumberConstraintName =
| NumberComparatorConstraintName
| `s.number.${'int' | 'safeInt' | 'finite' | 'eq(NaN)' | 'ne(NaN)' | 'divisibleBy'}`;

function numberComparator(comparator: Comparator, name: NumberComparatorConstraintName, expected: string, number: number): IConstraint<number> {
return {
run(input: number) {
return comparator(input, number) //
Expand Down
10 changes: 9 additions & 1 deletion src/constraints/StringConstraints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { Result } from '../lib/Result';
import type { IConstraint } from './base/IConstraint';
import { Comparator, eq, ge, gt, le, lt, ne } from './util/operators';

function stringLengthComparator(comparator: Comparator, name: string, expected: string, length: number): IConstraint<string> {
export type StringLengthComparatorConstraintName = `s.string.length${'Lt' | 'Le' | 'Gt' | 'Ge' | 'Eq' | 'Ne'}`;
export type StringConstraintName = StringLengthComparatorConstraintName;

function stringLengthComparator(
comparator: Comparator,
name: StringLengthComparatorConstraintName,
expected: string,
length: number
): IConstraint<string> {
return {
run(input: string) {
return comparator(input.length, length) //
Expand Down
60 changes: 60 additions & 0 deletions src/constraints/type-exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export type {
ArrayConstraintName,
ArrayLengthComparatorConstraintName,
arrayLengthEq,
arrayLengthGe,
arrayLengthGt,
arrayLengthLe,
arrayLengthLt,
arrayLengthNe
} from './ArrayLengthConstraints';
export type { IConstraint } from './base/IConstraint';
export type {
BigIntComparatorConstraintName,
BigIntConstraintName,
bigintEq,
bigintGe,
bigintGt,
bigintLe,
bigintLt,
bigintNe
} from './BigIntConstraints';
export type { BooleanConstraintName, booleanFalse, booleanTrue } from './BooleanConstraints';
export type {
DateComparatorConstraintName,
DateConstraintName,
dateEq,
dateGe,
dateGt,
dateInvalid,
dateLe,
dateLt,
dateNe,
dateValid
} from './DateConstraints';
export type {
NumberComparatorConstraintName,
NumberConstraintName,
numberDivisibleBy,
numberEq,
numberFinite,
numberGe,
numberGt,
numberInt,
numberLe,
numberLt,
numberNaN,
numberNe,
numberNeNaN,
numberSafeInt
} from './NumberConstraints';
export type {
StringConstraintName,
StringLengthComparatorConstraintName,
stringLengthEq,
stringLengthGe,
stringLengthGt,
stringLengthLe,
stringLengthLt,
stringLengthNe
} from './StringConstraints';
22 changes: 19 additions & 3 deletions src/lib/errors/ConstraintError.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { inspect, type InspectOptionsStylized } from 'node:util';
import type {
ArrayConstraintName,
BigIntConstraintName,
BooleanConstraintName,
DateConstraintName,
NumberConstraintName,
StringConstraintName
} from '../../constraints/type-exports';
import { BaseError, customInspectSymbolStackLess } from './BaseError';

export type ConstraintErrorNames =
| ArrayConstraintName
| BigIntConstraintName
| BooleanConstraintName
| DateConstraintName
| NumberConstraintName
| StringConstraintName;

export class ConstraintError<T = unknown> extends BaseError {
public readonly constraint: string;
public readonly constraint: ConstraintErrorNames;
public readonly given: T;
public readonly expected: string;

public constructor(validator: string, message: string, given: T, expected: string) {
public constructor(constraint: ConstraintErrorNames, message: string, given: T, expected: string) {
super(message);
this.constraint = validator;
this.constraint = constraint;
this.given = given;
this.expected = expected;
}
Expand Down
57 changes: 46 additions & 11 deletions src/type-exports.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
export type { arrayLengthEq, arrayLengthGe, arrayLengthGt, arrayLengthLe, arrayLengthLt, arrayLengthNe } from './constraints/ArrayLength';
export type { IConstraint } from './constraints/base/IConstraint';
export type { bigintEq, bigintGe, bigintGt, bigintLe, bigintLt, bigintNe } from './constraints/BigIntConstraints';
export type { booleanFalse, booleanTrue } from './constraints/BooleanConstraints';
export type { dateEq, dateGe, dateGt, dateInvalid, dateLe, dateLt, dateNe, dateValid } from './constraints/DateConstraints';
export type {
ArrayConstraintName,
ArrayLengthComparatorConstraintName,
arrayLengthEq,
arrayLengthGe,
arrayLengthGt,
arrayLengthLe,
arrayLengthLt,
arrayLengthNe,
BigIntComparatorConstraintName,
BigIntConstraintName,
bigintEq,
bigintGe,
bigintGt,
bigintLe,
bigintLt,
bigintNe,
BooleanConstraintName,
booleanFalse,
booleanTrue,
DateComparatorConstraintName,
DateConstraintName,
dateEq,
dateGe,
dateGt,
dateInvalid,
dateLe,
dateLt,
dateNe,
dateValid,
IConstraint,
NumberComparatorConstraintName,
NumberConstraintName,
numberDivisibleBy,
numberEq,
numberFinite,
numberGe,
Expand All @@ -14,13 +42,20 @@ export type {
numberNaN,
numberNe,
numberNeNaN,
numberSafeInt
} from './constraints/NumberConstraints';
export type { stringLengthEq, stringLengthGe, stringLengthGt, stringLengthLe, stringLengthLt, stringLengthNe } from './constraints/StringConstraints';
numberSafeInt,
StringConstraintName,
StringLengthComparatorConstraintName,
stringLengthEq,
stringLengthGe,
stringLengthGt,
stringLengthLe,
stringLengthLt,
stringLengthNe
} from './constraints/type-exports';
//
export type { BaseError } from './lib/errors/BaseError';
export type { CombinedError } from './lib/errors/CombinedError';
export type { ConstraintError } from './lib/errors/ConstraintError';
export type { ConstraintError, ConstraintErrorNames } from './lib/errors/ConstraintError';
export type { ExpectedValidationError } from './lib/errors/ExpectedValidationError';
export type { MissingPropertyError } from './lib/errors/MissingPropertyError';
export type { UnknownPropertyError } from './lib/errors/UnknownPropertyError';
Expand All @@ -35,8 +70,10 @@ export type { BaseValidator } from './validators/BaseValidator';
export type { BigIntValidator } from './validators/BigIntValidator';
export type { BooleanValidator } from './validators/BooleanValidator';
export type { DateValidator } from './validators/DateValidator';
export type { DefaultValidator } from './validators/DefaultValidator';
export type { InstanceValidator } from './validators/InstanceValidator';
export type { LiteralValidator } from './validators/LiteralValidator';
export type { MapValidator } from './validators/MapValidator';
export type { NeverValidator } from './validators/NeverValidator';
export type { NullishValidator } from './validators/NullishValidator';
export type { NumberValidator } from './validators/NumberValidator';
Expand All @@ -46,5 +83,3 @@ export type { RecordValidator } from './validators/RecordValidator';
export type { SetValidator } from './validators/SetValidator';
export type { StringValidator } from './validators/StringValidator';
export type { UnionValidator } from './validators/UnionValidator';
export type { MapValidator } from './validators/MapValidator';
export type { DefaultValidator } from './validators/DefaultValidator';

0 comments on commit 41bebea

Please sign in to comment.