OpenForm

percentage

Last updated on

Validate percentage values

Validate percentage values. The Percentage primitive ensures numeric values represent valid percentages with optional range constraints.

Examples

Creating percentages

// Direct validation
const pct = open.percentage(75.5)

// Parse with options
const pct = open.percentage.parse(input, { min: 0, max: 100 })

// Convert from decimal (0.755 → 75.5)
const pct = open.percentage.fromDecimal(0.755)

// Convert to decimal (75.5 → 0.755)
const decimal = open.percentage.toDecimal(75.5)

// Check validity without throwing
if (open.percentage.isValid(50)) {
  console.log('Valid percentage')
}

Parsing external data

// Parse unknown input (throws on error)
const pct = open.percentage.parse(userInput)

// Safe parsing (returns result object)
const result = open.percentage.safeParse(userInput)
if (result.success) {
  console.log(`Percentage: ${result.data}%`)
} else {
  console.error(result.error.message)
}

// Parse with range constraints
const pct = open.percentage.parse(userInput, { min: 0, max: 100 })

Using with forms

const form = open.form({
  name: 'discount',
  fields: {
    discountRate: { type: 'percentage', label: 'Discount Rate', min: 0, max: 50 },
    taxRate: { type: 'percentage', label: 'Tax Rate' }
  }
})

const filled = form.fill({
  fields: {
    discountRate: open.percentage(15),
    taxRate: open.percentage(8.25)
  }
})

API

Direct Call

open.percentage(input: number): number

Pass a number for validation. Returns the validated number or throws on error.

const pct = open.percentage(75.5)

Static Methods

parse: (input: unknown, options?: PercentageOptions) => number
Parse unknown input (throws on error)
safeParse: (input: unknown, options?: PercentageOptions) => Result<number>
Parse unknown input (returns result object)
isValid: (input: number) => boolean
Check if a number is a valid percentage
fromDecimal: (decimal: number) => number
Convert decimal to percentage (0.5 → 50)
toDecimal: (percentage: number) => number
Convert percentage to decimal (50 → 0.5)

PercentageOptions

OptionTypeDescription
minnumber?Minimum allowed value
maxnumber?Maximum allowed value
precisionnumber?Decimal places to round to (default: 2)

Conversion Helpers

// Decimal to percentage
open.percentage.fromDecimal(0.5)    // → 50
open.percentage.fromDecimal(0.125)  // → 12.5
open.percentage.fromDecimal(1.5)    // → 150

// Percentage to decimal
open.percentage.toDecimal(50)       // → 0.5
open.percentage.toDecimal(12.5)     // → 0.125
open.percentage.toDecimal(150)      // → 1.5

Validation

The Percentage primitive validates:

  • Value must be a finite number (not NaN or Infinity)
  • Value must satisfy min/max constraints if provided
// Valid
open.percentage(0)
open.percentage(100)
open.percentage(50.5)
open.percentage(-10)   // Negative allowed by default
open.percentage(150)   // Over 100 allowed by default

// With constraints
open.percentage.parse(50, { min: 0, max: 100 })  // Valid
open.percentage.parse(150, { min: 0, max: 100 }) // Throws

// Invalid - throws Error
open.percentage(NaN)
// Error: Invalid Percentage: value must be a valid number

open.percentage(Infinity)
// Error: Invalid Percentage: value cannot be Infinity

Related

On this page