-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
54 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Types } from '../../types/schema'; | ||
import { NumberBasedSchemaKey } from './NumberBasedSchemaKey'; | ||
|
||
export class BigIntSchemaKey extends NumberBasedSchemaKey<Types.BigInt> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Expression, Types } from '../../types/schema'; | ||
import { SchemaKey } from './SchemaKey'; | ||
|
||
export abstract class NumberBasedSchemaKey< | ||
T extends Types.Number | Types.BigInt, | ||
> extends SchemaKey<T> { | ||
public min( | ||
number: number, | ||
message = `Min length of the "${Expression.Key}" must be ${number}` | ||
) { | ||
return this.effect((num) => num > number, message); | ||
} | ||
|
||
public max( | ||
number: number, | ||
message = `Max length of the "${Expression.Key}" must be ${number}` | ||
) { | ||
return this.effect((num) => num < number, message); | ||
} | ||
|
||
public gte( | ||
number: number, | ||
message = `"${Expression.Key}" must be greater or equal than ${number}` | ||
) { | ||
return this.min(number - 1, message); | ||
} | ||
|
||
public lte( | ||
number: number, | ||
message = `"${Expression.Key}" must be less or equal to ${number}` | ||
) { | ||
return this.max(number - 1, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,12 @@ | ||
import { Types, Expression } from '../../types/schema'; | ||
import { SchemaKey } from './SchemaKey'; | ||
import { NumberBasedSchemaKey } from './NumberBasedSchemaKey'; | ||
|
||
export class NumberSchemaKey extends SchemaKey<Types.Number> { | ||
export class NumberSchemaKey extends NumberBasedSchemaKey<Types.Number> { | ||
public integer(message = `"${Expression.Key}" must be a integer`) { | ||
return this.effect(Number.isSafeInteger, message); | ||
} | ||
|
||
public float(message = `"${Expression.Key}" me be a float`) { | ||
return this.effect((num) => !Number.isInteger(num), message); | ||
} | ||
|
||
public min( | ||
number: number, | ||
message = `Min length of the "${Expression.Key}" must be ${number}` | ||
) { | ||
return this.effect((num) => num > number, message); | ||
} | ||
|
||
public max( | ||
number: number, | ||
message = `Max length of the "${Expression.Key}" must be ${number}` | ||
) { | ||
return this.effect((num) => num < number, message); | ||
} | ||
|
||
public gte( | ||
number: number, | ||
message = `"${Expression.Key}" must be greater or equal than ${number}` | ||
) { | ||
return this.min(number - 1, message); | ||
} | ||
|
||
public lte( | ||
number: number, | ||
message = `"${Expression.Key}" must be less or equal to ${number}` | ||
) { | ||
return this.max(number - 1, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters