|
| 1 | +/* |
| 2 | + * @adonisjs/validator |
| 3 | + * |
| 4 | + * (c) Harminder Virk <virk@adonisjs.com> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import { test } from '@japa/runner' |
| 11 | +import { rules } from '../../src/Rules' |
| 12 | +import { validate } from '../fixtures/rules/index' |
| 13 | +import { MessagesBag } from '../../src/MessagesBag' |
| 14 | +import { ApiErrorReporter } from '../../src/ErrorReporter' |
| 15 | +import { bigint } from '../../src/Validations/primitives/bigint' |
| 16 | + |
| 17 | +function compile() { |
| 18 | + return bigint.compile('literal', 'bigint', rules['bigint']().options, {}) |
| 19 | +} |
| 20 | + |
| 21 | +test.group('BigInt', () => { |
| 22 | + validate(bigint, test, 'helloworld', 10n, compile()) |
| 23 | + |
| 24 | + test('report error when value is near Infinity', ({ assert }) => { |
| 25 | + const reporter = new ApiErrorReporter(new MessagesBag({}), false) |
| 26 | + bigint.validate( |
| 27 | + '-3177777777777777777777777777777777777777777777777777777777777777777777777770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999991111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111', |
| 28 | + compile().compiledOptions, |
| 29 | + { |
| 30 | + errorReporter: reporter, |
| 31 | + field: 'age', |
| 32 | + pointer: 'age', |
| 33 | + tip: {}, |
| 34 | + root: {}, |
| 35 | + refs: {}, |
| 36 | + mutate: () => {}, |
| 37 | + } |
| 38 | + ) |
| 39 | + |
| 40 | + assert.deepEqual(reporter.toJSON(), { |
| 41 | + errors: [ |
| 42 | + { |
| 43 | + field: 'age', |
| 44 | + rule: 'bigint', |
| 45 | + message: 'bigint validation failed', |
| 46 | + }, |
| 47 | + ], |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + test('report error when value is not a valid bigint', ({ assert }) => { |
| 52 | + const reporter = new ApiErrorReporter(new MessagesBag({}), false) |
| 53 | + bigint.validate(null, compile().compiledOptions, { |
| 54 | + errorReporter: reporter, |
| 55 | + field: 'age', |
| 56 | + pointer: 'age', |
| 57 | + tip: {}, |
| 58 | + root: {}, |
| 59 | + refs: {}, |
| 60 | + mutate: () => {}, |
| 61 | + }) |
| 62 | + |
| 63 | + assert.deepEqual(reporter.toJSON(), { |
| 64 | + errors: [ |
| 65 | + { |
| 66 | + field: 'age', |
| 67 | + rule: 'bigint', |
| 68 | + message: 'bigint validation failed', |
| 69 | + }, |
| 70 | + ], |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + test('cast bigint like string to a valid bigint', ({ assert }) => { |
| 75 | + const reporter = new ApiErrorReporter(new MessagesBag({}), false) |
| 76 | + let value: any = '21' |
| 77 | + |
| 78 | + bigint.validate(value, compile().compiledOptions, { |
| 79 | + errorReporter: reporter, |
| 80 | + field: 'age', |
| 81 | + pointer: 'age', |
| 82 | + tip: {}, |
| 83 | + root: {}, |
| 84 | + refs: {}, |
| 85 | + mutate: (newValue) => { |
| 86 | + value = newValue |
| 87 | + }, |
| 88 | + }) |
| 89 | + |
| 90 | + assert.deepEqual(reporter.toJSON(), { errors: [] }) |
| 91 | + assert.equal(value, 21n) |
| 92 | + }) |
| 93 | + |
| 94 | + test('work fine when value is a valid bigint', ({ assert }) => { |
| 95 | + const reporter = new ApiErrorReporter(new MessagesBag({}), false) |
| 96 | + bigint.validate(21n, compile().compiledOptions, { |
| 97 | + errorReporter: reporter, |
| 98 | + field: 'age', |
| 99 | + pointer: 'age', |
| 100 | + tip: {}, |
| 101 | + root: {}, |
| 102 | + refs: {}, |
| 103 | + mutate: () => {}, |
| 104 | + }) |
| 105 | + |
| 106 | + assert.deepEqual(reporter.toJSON(), { errors: [] }) |
| 107 | + }) |
| 108 | + |
| 109 | + test('report error when value is a string that cannot be casted to a bigint', ({ assert }) => { |
| 110 | + const reporter = new ApiErrorReporter(new MessagesBag({}), false) |
| 111 | + bigint.validate('hello-world', compile().compiledOptions, { |
| 112 | + errorReporter: reporter, |
| 113 | + field: 'age', |
| 114 | + pointer: 'age', |
| 115 | + tip: {}, |
| 116 | + root: {}, |
| 117 | + refs: {}, |
| 118 | + mutate: () => {}, |
| 119 | + }) |
| 120 | + |
| 121 | + assert.deepEqual(reporter.toJSON(), { |
| 122 | + errors: [ |
| 123 | + { |
| 124 | + field: 'age', |
| 125 | + rule: 'bigint', |
| 126 | + message: 'bigint validation failed', |
| 127 | + }, |
| 128 | + ], |
| 129 | + }) |
| 130 | + }) |
| 131 | +}) |
0 commit comments