Skip to content

$countSameRooms

Counts how many other Rooms in the model share the same Room Number as the given Room. Used to detect duplicate room numbers.


Definition

$countSameRooms := function($roomNumber, $roomId){
  $count($Rooms[values.number = $roomNumber and id != $roomId])
};

Dependencies

Requires $Rooms to be defined in the outer scope:

$Rooms := $[type = "Room"];

Pre-collecting all rooms into a variable avoids re-evaluating the full dataset filter for every room.


Parameters

Parameter Type Description
$roomNumber string/number The number of the room being checked
$roomId number The id of the room being checked — excluded from the count so a room does not count itself

Return value

A number: 0 if the number is unique, 1 or higher if duplicates exist.


Usage

(
  $Rooms := $[type = "Room"];

  $countSameRooms := function($roomNumber, $roomId){
    $count($Rooms[values.number = $roomNumber and id != $roomId])
  };

  $[type = "Room"].{
    "id": id,
    "name": name,
    "number": values.number,
    "numOfOtherRooms": $countSameRooms(values.number, id)
  }
)

Validator: numOfOtherRoomsint:EqualTo 0


Used in rules

  • rule-b2b611d9 — Room number is uniek