OpenForm

rating

Last updated on

Validate rating values

Validate rating values. The Rating primitive ensures numeric values represent valid ratings with optional step alignment and range constraints.

Examples

Creating ratings

// Direct validation (defaults: min=1, max=5, step=1)
const rating = open.rating(4)

// Parse with options for half-star ratings
const rating = open.rating.parse(input, { min: 1, max: 5, step: 0.5 })

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

Parsing external data

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

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

// Parse with constraints
const rating = open.rating.parse(userInput, {
  min: 1,
  max: 5,
  step: 0.5  // Only allow 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5
})

Using with forms

const form = open.form({
  name: 'review',
  fields: {
    overallRating: { type: 'rating', label: 'Overall Rating', min: 1, max: 5 },
    serviceRating: { type: 'rating', label: 'Service Rating', min: 1, max: 10 }
  }
})

const filled = form.fill({
  fields: {
    overallRating: open.rating(4),
    serviceRating: open.rating(8)
  }
})

API

Direct Call

open.rating(input: number): number

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

const rating = open.rating(4)

Static Methods

parse: (input: unknown, options?: RatingOptions) => number
Parse unknown input (throws on error)
safeParse: (input: unknown, options?: RatingOptions) => Result<number>
Parse unknown input (returns result object)
isValid: (input: number) => boolean
Check if a number is a valid rating

RatingOptions

OptionTypeDescription
minnumber?Minimum allowed value
maxnumber?Maximum allowed value
stepnumber?Step increment (e.g., 0.5 for half-star ratings)

Step Alignment

When a step is specified, ratings must align to the step value:

// Step of 1 (whole stars only)
open.rating.parse(4, { step: 1 })      // Valid: 4
open.rating.parse(4.5, { step: 1 })    // Throws: not aligned

// Step of 0.5 (half stars)
open.rating.parse(4, { step: 0.5 })    // Valid: 4
open.rating.parse(4.5, { step: 0.5 })  // Valid: 4.5
open.rating.parse(4.3, { step: 0.5 })  // Throws: not aligned

// Step of 0.1 (tenths)
open.rating.parse(4.7, { step: 0.1 })  // Valid: 4.7

Common Rating Scales

ScaleRangeStepExample
5-star1-50.5 or 1Movie ratings
10-point1-101Product reviews
Percentage0-1001Satisfaction scores
NPS0-101Net Promoter Score

Validation

The Rating primitive validates:

  • Value must be a finite number (not NaN or Infinity)
  • Value must satisfy min/max constraints (defaults: min=1, max=5)
  • Value must align to step (default: step=1, whole numbers only)
// Valid with defaults (min=1, max=5, step=1)
open.rating(1)
open.rating(3)
open.rating(5)

// With custom options
open.rating.parse(4.5, { min: 1, max: 5, step: 0.5 }) // Valid: half-stars enabled
open.rating.parse(0, { min: 0, max: 5 })              // Valid: custom min

// Invalid with defaults - throws Error
open.rating(0)
// Error: 0 is outside the valid range (1-5)

open.rating(4.5)
// Error: 4.5 does not align with step size 1

open.rating(NaN)
// Error: value must be a finite number

open.rating.parse(6, { min: 1, max: 5 })
// Error: 6 is outside the valid range (1-5)

Related

On this page