@nori-zk/proof-conversion
Version:
Verifying zkVM proofs inside o1js circuits, to generate Mina compatible proof
166 lines • 5.79 kB
JavaScript
import { guard } from './core.js';
// ============================================================================
// PRIMITIVE GUARDS - No constraints, just type checking
// ============================================================================
/**
* Type guard validator that checks if a value is undefined.
* Returns a ValidatorFn that narrows the type to `undefined` on success.
*
* @returns ValidatorFn<undefined> - Validator function with type predicate
*
* @example
* const validator = isUndefined;
* if (validator(value)) {
* // value is narrowed to type: undefined
* }
*/
export const isUndefined = guard(function isUndefined(val) {
return val === undefined;
});
/**
* Type guard validator that checks if a value is null.
* Returns a ValidatorFn that narrows the type to `null` on success.
*
* @returns ValidatorFn<null> - Validator function with type predicate
*
* @example
* const validator = isNull;
* if (validator(value)) {
* // value is narrowed to type: null
* }
*/
export const isNull = guard(function isNull(val) {
return val === null;
});
/**
* Type guard validator that checks if a value is a string.
* Returns a ValidatorFn that narrows the type to `string` on success.
*
* @returns ValidatorFn<string> - Validator function with type predicate
*
* @example
* const validator = isString;
* if (validator(value)) {
* // value is narrowed to type: string
* const length = value.length;
* }
*/
export const isString = guard(function isString(val) {
return typeof val === 'string';
});
/**
* Type guard validator that checks if a value is a number.
* Returns a ValidatorFn that narrows the type to `number` on success.
*
* @returns ValidatorFn<number> - Validator function with type predicate
*
* @example
* const validator = isNumber;
* if (validator(value)) {
* // value is narrowed to type: number
* const doubled = value * 2;
* }
*/
export const isNumber = guard(function isNumber(val) {
return typeof val === 'number';
});
/**
* Type guard validator that checks if a value is a boolean.
* Returns a ValidatorFn that narrows the type to `boolean` on success.
*
* @returns ValidatorFn<boolean> - Validator function with type predicate
*
* @example
* const validator = isBoolean;
* if (validator(value)) {
* // value is narrowed to type: boolean
* const negated = !value;
* }
*/
export const isBoolean = guard(function isBoolean(val) {
return typeof val === 'boolean';
});
/**
* Factory function that creates a type guard validator for bounded numbers.
* Returns a ValidatorFn that narrows the type to `number` and validates range constraints.
*
* The returned validator checks that a value is a number and optionally within specified bounds.
* Provides detailed diagnostic messages for validation failures.
*
* @param constraint - Min/max bounds for the number
* @returns ValidatorFn<number> - Validator function with type predicate and constraint info
*
* @example
* const isPositive = isBoundedNumber({ min: 0 });
* const isPercentage = isBoundedNumber({ min: 0, max: 100 });
*
* if (isPositive(value)) {
* // value is narrowed to type: number (and >= 0 at runtime)
* }
*/
export const isBoundedNumber = (constraint) => guard(function isBoundedNumber(val) {
if (typeof val !== 'number')
return false;
if (constraint.min !== undefined && val < constraint.min)
return false;
if (constraint.max !== undefined && val > constraint.max)
return false;
return true;
}, {
constraint,
printConstraint: (c) => `(${c.min ?? '-∞'}..${c.max ?? '∞'})`,
printDiagnosis: (c, val) => {
if (typeof val !== 'number')
return `got ${val === null ? 'null' : Array.isArray(val) ? `Array[${val.length}]` : typeof val}`;
if (c.min !== undefined && val < c.min)
return `got ${val} which is below minimum ${c.min}`;
if (c.max !== undefined && val > c.max)
return `got ${val} which exceeds maximum ${c.max}`;
return `got ${val}`;
},
});
/**
* Type guard validator for uint8 (unsigned 8-bit integer).
* Returns a ValidatorFn that narrows the type to `number` and validates 0..255 range.
*
* @returns ValidatorFn<number> - Validator function for numbers in range [0, 255]
*
* @example
* const validator = isUint8;
* if (validator(value)) {
* // value is narrowed to type: number (and 0..255 at runtime)
* }
*/
export const isUint8 = isBoundedNumber({ min: 0, max: 255 });
/**
* Creates a number validator with type-level literal unions for small ranges.
*
* Returns a union of exact number literals: `0 | 1 | 2 | ... | Max`.
* Only use for small ranges (< 10) to avoid TypeScript issues.
*
* @example
* const isSmallNumber = isBoundedNumberUnion({ min: 0, max: 3 });
* // Returns: (val: unknown) => val is 0 | 1 | 2 | 3
*/
export const isBoundedNumberUnion = (options) => guard(function isBoundedNumberUnion(val) {
if (typeof val !== 'number')
return false;
if (options.min !== undefined && val < options.min)
return false;
if (options.max !== undefined && val > options.max)
return false;
return true;
}, {
constraint: options,
printConstraint: (c) => `(${c.min ?? '-∞'}..${c.max ?? '∞'})`,
printDiagnosis: (c, val) => {
if (typeof val !== 'number')
return `got ${val === null ? 'null' : Array.isArray(val) ? `Array[${val.length}]` : typeof val}`;
if (c.min !== undefined && val < c.min)
return `got ${val} which is below minimum ${c.min}`;
if (c.max !== undefined && val > c.max)
return `got ${val} which exceeds maximum ${c.max}`;
return `got ${val}`;
},
});
//# sourceMappingURL=primitives.js.map