UNPKG

@schemasjs/valibot-numbers

Version:
185 lines (184 loc) 5.93 kB
// src/index.ts import * as v from "valibot"; var UnsignedIntegerSchema = v.pipe(v.number("It should be a number"), v.integer("It should be an integer"), v.minValue(0, "It should be greater than 0")); var UINT_MIN = 0; var UINT8_MAX = Math.pow(2, 8) - 1; var Uint8Schema = v.pipe( v.number("It should be a number"), v.integer("It should be an integer number"), v.minValue(0, "It should be greater than 0"), v.maxValue(UINT8_MAX, `It should be less than ${UINT8_MAX + 1}`) ); var UINT16_MAX = Math.pow(2, 16) - 1; var Uint16Schema = v.pipe( v.number("It should be a number"), v.integer("It should be an integer number"), v.minValue(0, "It should be greater than 0"), v.maxValue(UINT16_MAX, `It should be less than ${UINT16_MAX + 1}`) ); var UINT32_MAX = Math.pow(2, 32) - 1; var Uint32Schema = v.pipe( v.number("It should be a number"), v.integer("It should be an integer number"), v.minValue(0, "It should be greater than 0"), v.maxValue(UINT32_MAX, `It should be less than ${UINT32_MAX + 1}`) ); var BigUintSchema = v.pipe(v.bigint(), v.minValue(0n, "It should be greater than 0")); var IntegerSchema = v.pipe(v.number("It should be a number"), v.integer("It should be an integer")); var INT8_MIN = -Math.pow(2, 7); var INT8_MAX = -INT8_MIN - 1; var Int8Schema = v.pipe( v.number("It should be a number"), v.integer("It should be an integer number"), v.minValue(INT8_MIN, `It should be greater than ${INT8_MIN - 1}`), v.maxValue(INT8_MAX, `It should be less than ${INT8_MAX + 1}`) ); var INT16_MIN = -Math.pow(2, 15); var INT16_MAX = -INT16_MIN - 1; var Int16Schema = v.pipe( v.number("It should be a number"), v.integer("It should be an integer number"), v.minValue(INT16_MIN, `It should be greater than ${INT16_MIN - 1}`), v.maxValue(INT16_MAX, `It should be less than ${INT16_MAX + 1}`) ); var INT32_MIN = -Math.pow(2, 31); var INT32_MAX = -INT32_MIN - 1; var Int32Schema = v.pipe( v.number("It should be a number"), v.integer("It should be an integer number"), v.minValue(INT32_MIN, `It should be greater than ${INT32_MIN - 1}`), v.maxValue(INT32_MAX, `It should be less than ${INT32_MAX + 1}`) ); var BigIntSchema = v.bigint(); var FLOAT32_MIN = -34e37; var FLOAT32_MAX = -FLOAT32_MIN; var Float32Schema = v.pipe( v.number(), v.check((num) => { const arr = Float32Array.from([num])[0]; const diff = Math.abs(arr - num); const error = Math.abs(num / 10); return diff < error; }, "Invalid number, it is not a Float32 number") // v.minValue(FLOAT32_MIN, `It should be greater than or equal to ${FLOAT32_MIN}`), // v.maxValue(FLOAT32_MAX, `It should be less than or equal to ${FLOAT32_MAX}`) ); var FLOAT64_MIN = -1 * Number.MAX_VALUE; var FLOAT64_MAX = -FLOAT64_MIN; var Float64Schema = v.pipe( v.number(), v.check((num) => { const arr = Float64Array.from([num])[0]; const diff = Math.abs(arr - num); const error = Math.abs(num / 10); return diff < error; }, "Invalid number, it is not a Float64 number") // v.minValue(FLOAT64_MIN, `It should be greater than or equal to ${FLOAT64_MIN}`), // v.maxValue(FLOAT64_MAX, `It should be less than or equal to ${FLOAT64_MAX}`) ); var passToArray = (value, min, max) => { const array = []; let num = value; if (num > 0) { while (num > max) { array.push(max); num -= max; } } else if (num < 0) { while (num < min) { array.push(min); num -= min; } } array.push(num); return array; }; var Uint8ArraySchema = v.instance(Uint8Array); var Uint8ArrayTransform = v.pipe( UnsignedIntegerSchema, v.transform((input) => Uint8Array.from(passToArray(input, UINT_MIN, UINT8_MAX))) ); var Uint16ArraySchema = v.instance(Uint16Array); var Uint16ArrayTransform = v.pipe( UnsignedIntegerSchema, v.transform((input) => Uint16Array.from(passToArray(input, UINT_MIN, UINT16_MAX))) ); var Uint32ArraySchema = v.instance(Uint32Array); var Uint32ArrayTransform = v.pipe( UnsignedIntegerSchema, v.transform((input) => Uint32Array.from(passToArray(input, UINT_MIN, UINT32_MAX))) ); var BigUint64ArraySchema = v.instance(BigUint64Array); var Int8ArraySchema = v.instance(Int8Array); var Int8ArrayTransform = v.pipe( IntegerSchema, v.transform((input) => Int8Array.from(passToArray(input, INT8_MIN, INT8_MAX))) ); var Int16ArraySchema = v.instance(Int16Array); var Int16ArrayTransform = v.pipe( IntegerSchema, v.transform((input) => Int16Array.from(passToArray(input, INT16_MIN, INT16_MAX))) ); var Int32ArraySchema = v.instance(Int32Array); var Int32ArrayTransform = v.pipe( IntegerSchema, v.transform((input) => Int32Array.from(passToArray(input, INT32_MIN, INT32_MAX))) ); var BigInt64ArraySchema = v.instance(BigInt64Array); var Float32ArraySchema = v.instance(Float32Array); var Float32ArrayTransform = v.pipe( v.number(), v.transform((input) => Float32Array.from(passToArray(input, FLOAT32_MIN, FLOAT32_MAX))) ); var Float64ArraySchema = v.instance(Float64Array); var Float64ArrayTransform = v.pipe( v.number(), v.transform((input) => Float64Array.from(passToArray(input, FLOAT64_MIN, FLOAT64_MAX))) ); export { BigInt64ArraySchema, BigIntSchema, BigUint64ArraySchema, BigUintSchema, FLOAT32_MAX, FLOAT32_MIN, FLOAT64_MAX, FLOAT64_MIN, Float32ArraySchema, Float32ArrayTransform, Float32Schema, Float64ArraySchema, Float64ArrayTransform, Float64Schema, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT8_MAX, INT8_MIN, Int16ArraySchema, Int16ArrayTransform, Int16Schema, Int32ArraySchema, Int32ArrayTransform, Int32Schema, Int8ArraySchema, Int8ArrayTransform, Int8Schema, IntegerSchema, UINT16_MAX, UINT32_MAX, UINT8_MAX, UINT_MIN, Uint16ArraySchema, Uint16ArrayTransform, Uint16Schema, Uint32ArraySchema, Uint32ArrayTransform, Uint32Schema, Uint8ArraySchema, Uint8ArrayTransform, Uint8Schema, UnsignedIntegerSchema, passToArray };