import * as funtypes from 'funtypes'
const BigIntParser: funtypes.ParsedValue<funtypes.String, bigint>['config'] = {
parse: value => {
if (!/^0x([a-fA-F0-9]{1,64})$/.test(value)) return { success: false, message: `${value} is not a hex string encoded number.` }
else return { success: true, value: BigInt(value) }
},
serialize: value => {
if (typeof value !== 'bigint') return { success: false, message: `${typeof value} is not a bigint.`}
return { success: true, value: `0x${value.toString(16)}` }
},
}
export const BigIntType = funtypes.String.withParser(BigIntParser).withConstraint((a) => { console.log(a); return true })
console.log('serializing')
BigIntType.serialize(10n)
console.log('parsing:')
BigIntType.parse('0x10')
returns output:
serializing
0xa
parsing:
16n
The prints happen inside constraint checking, and you can see that in serializing step, the constraint check is done for strings, while in parsing check, its done for a bigint, This means that the serialize runs and then constraint is checked, and with parsing the parsing is first completed and then constraints are checked.
The workaround for this is to make constraint checker to check what is the type and perform the constraint checking with that datatype.
Would it be possible to preform constraint checking with the same datatype for both parsing and serializing? This would let user to only write one constraint checker function
returns output:
The prints happen inside constraint checking, and you can see that in serializing step, the constraint check is done for strings, while in parsing check, its done for a bigint, This means that the serialize runs and then constraint is checked, and with parsing the parsing is first completed and then constraints are checked.
The workaround for this is to make constraint checker to check what is the type and perform the constraint checking with that datatype.
Would it be possible to preform constraint checking with the same datatype for both parsing and serializing? This would let user to only write one constraint checker function