feat(review): improved rating validation

This commit is contained in:
nostrdev-com 2025-04-09 15:39:43 +03:00
parent 5e01078d7a
commit aa82179788
3 changed files with 16 additions and 5 deletions
src
models
types
utils/validation

@ -1,10 +1,11 @@
import { ObjectId } from 'mongodb'
import { RatingOptions } from '../types'
export class Review {
constructor(
public eventId: string, // foreign key referencing the nostrEvents collection
public productId: string, // unique identifier for the product
public rating: number, // numerical rating, e.g., 1-100
public rating: number | RatingOptions, // numerical rating, e.g., 84-100 or NS (no score)
public reviewText: string, // text content of the review
public tastingNotes: string[], // array of tasting notes, e.g., flavours, aromas
public id?: ObjectId // database object id

@ -1,4 +1,4 @@
export type Availability = 'in stock' | 'out of stock' | 'discontinued'
export type Availability = 'In stock' | 'Out of stock' | 'Discontinued'
export type Ingredient =
| 'Blanche'
@ -62,11 +62,15 @@ export type Ingredient =
| 'Walnut'
export enum VintageOptions {
NV = 'nv',
MV = 'mv'
NV = 'NV',
MV = 'MV'
}
export interface StandardDrinks {
'100ml': { AU: number; UK: number; US: number }
bottle: { AU: number; UK: number; US: number }
}
export enum RatingOptions {
NoScore = 'NS'
}

@ -1,10 +1,16 @@
import Joi from 'joi'
import { RatingOptions } from '../../types'
export const reviewValidation = (data: unknown): Joi.ValidationResult =>
Joi.object({
eventId: Joi.string().required(),
productId: Joi.string().required(),
rating: Joi.number().required(),
rating: Joi.alternatives()
.try(
Joi.string().valid(...Object.values(RatingOptions)),
Joi.number().min(84).max(100)
)
.required(),
reviewText: Joi.string().required(),
tastingNotes: Joi.array().items(Joi.string())
}).validate(data)