Extending Existing Validations

In this lesson, you’ll learn how to extend and customize existing validations in Remult. Validations in Remult are simply functions that you can call and combine as needed.

Example: Unique Title Validation

Let’s extend the existing unique validation to check that no two tasks exist with the same title, as long as the title is not empty.

shared/Task.ts
5 collapsed lines
import { Entity, Fields, Validators } from 'remult'
@Entity('tasks', {
allowApiCrud: true,
})
export class Task {
@Fields.uuid()
id = ''
@Fields.string<Task>({
validate: (task, e) => {
if (task.title != '') return Validators.unique(task, e)
},
})
title = ''
7 collapsed lines
@Fields.boolean()
completed = false
@Fields.createdAt()
createdAt?: Date
}

Code Explanation

  • The validate function checks if the title is not empty.
  • If the title is not empty, the Validators.unique function is called to ensure that the task title is unique within the entity.
  • This approach allows you to combine and extend existing validations to suit your application’s needs.

Try It Out

Test this extended validation by trying to add tasks with duplicate titles. Notice that the validation prevents duplicates only when the title is not empty.

Powered by WebContainers
Files
Preparing Environment
  • Installing dependencies
  • Starting http server