Skip to content

Object self validation

By default, Regle validates the nested properties of an object. But sometimes you need to validate the object itself, to perform cross-field validation that depends on multiple properties.

The $self property allows you to apply validation rules directly to an object, giving you full control over object-level validation.

Basic usage

Use $self to define rules that validate the object itself rather than its nested properties.

ts
import {  } from 'vue';
import { , type  } from '@regle/core';
import { , , ,  } from '@regle/rules';

type  = {
  : string;
  : string;
};

const  = <{ :  }>({
  : {
    : '',
    : '',
  },
});

const {  } = (, {
  : {
    : {
      // Custom rule: at least one name must be filled
      : (
        (: <>) => (?.) || (?.),
        'At least one name is required'
      ),
    },
    : { : (3) },
    : { : (3) },
  },
});

In this example the atLeastOneName custom rule validates that at least one of firstName or lastName is filled.

Accessing $self status

The $self status is accessible through r$.fieldName.$self and provides all standard field status properties:

vue
<template>
  <ul>
    <li v-for="error of r$.$errors.user.$self" :key="error">
      {{ error }}
    </li>
  </ul>
</template>

Result:

Released under the MIT License. Logo by Johannes Lacourly