ratingLast updated on
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): numberPass 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
| Option | Type | Description |
|---|---|---|
min | number? | Minimum allowed value |
max | number? | Maximum allowed value |
step | number? | 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.7Common Rating Scales
| Scale | Range | Step | Example |
|---|---|---|---|
| 5-star | 1-5 | 0.5 or 1 | Movie ratings |
| 10-point | 1-10 | 1 | Product reviews |
| Percentage | 0-100 | 1 | Satisfaction scores |
| NPS | 0-10 | 1 | Net 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
- Primitives Overview - All available primitives
- Percentage - Percentage values
- Rating Field - Rating field type