-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
1 parent
aaeb7ff
commit 81aa868
Showing
7 changed files
with
358 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* @adonisjs/validator | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { SyncValidation } from '../../types.js' | ||
import { wrapCompile } from '../../validator/helpers.js' | ||
|
||
const DEFAULT_MESSAGE = 'bigint validation failed' | ||
const RULE_NAME = 'bigint' | ||
|
||
/** | ||
* Ensure the value is a valid bigint. Numeric string will be casted | ||
* to valid bigint | ||
*/ | ||
export const bigint: SyncValidation = { | ||
compile: wrapCompile(RULE_NAME), | ||
validate(value, _, { mutate, errorReporter, pointer, arrayExpressionPointer }) { | ||
if (typeof value === 'bigint') { | ||
return | ||
} | ||
|
||
/** | ||
* Report error when value is not a bigint and neither a string or a number | ||
*/ | ||
if (typeof value !== 'string' && typeof value !== 'number') { | ||
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer) | ||
return | ||
} | ||
|
||
/** | ||
* Attempt to cast string or number to a bigint. In case of | ||
* failure report the validation error | ||
*/ | ||
try { | ||
const castedValue = BigInt(value) | ||
|
||
/** | ||
* Mutate the value | ||
*/ | ||
mutate(castedValue) | ||
} catch (e) { | ||
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer) | ||
} | ||
}, | ||
} |
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
Oops, something went wrong.