Skip to content

Commit

Permalink
feat: stringify bigint, addresses issue gcanti#648
Browse files Browse the repository at this point in the history
  • Loading branch information
jonball4 committed Nov 8, 2022
1 parent e619fb1 commit 5ee57b4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PathReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function stringify(v: any): string {
}
return v > 0 ? 'Infinity' : '-Infinity'
}
return JSON.stringify(v)
return JSON.stringify(v, (_key: string, value: unknown) => (typeof value === 'bigint' ? `BigInt(${value})` : value))
}

function getContextPath(context: Context): string {
Expand Down
25 changes: 25 additions & 0 deletions test/PathReporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as assert from 'assert'
import { PathReporter } from '../src/PathReporter'
import { bigint, brand, Branded } from '../src'
import { struct } from '../src/Type'

describe('PathReporter', () => {
it('should properly report decoding errors against a bigint value', () => {
interface PositiveBigIntBrand {
readonly PositiveBigInt: unique symbol
}

const PositiveBigInt = brand(
bigint,
(n: bigint): n is Branded<bigint, PositiveBigIntBrand> => BigInt(0) < n,
'PositiveBigInt'
)

const testC = struct({ value: PositiveBigInt })
const testValidation = testC.decode({ value: BigInt(-10) })

const result = PathReporter.report(testValidation).join('\n')

assert.equal(result, 'Invalid value "BigInt(-10)" supplied to : { value: PositiveBigInt }/value: PositiveBigInt')
})
})

0 comments on commit 5ee57b4

Please sign in to comment.