exactlyOne

Full name: taxi.stdlib.exactlyOne

Signature

declare extension function <T> exactlyOne(collection:T[]):T

Description

Returns the only item from the provided collection, or throws an error if there isn’t exactly one item in the collection.

This function is useful when you expect a collection to contain precisely one element and want to retrieve it.

// Get the single active session for a user
find { user: User } as {
   currentSession: user.sessions.filter((session) -> session.isActive).exactlyOne()
}

Examples

This example demonstrates how to use exactlyOne() to retrieve the single active session for a user.

Try it out

Schema
Query Plan
Play with this snippet by editing it here, or edit it on Taxi Playground
Result
Query failed
Next
Welcome to Taxi