Skip to content

Commit f17772b

Browse files
committed
Adds description of new key_def functions comparing data against the key_def object rules
Since 3.1.0, new functions are added to ```key_def``` module. ```validate_key(key)``` - validates input key against the rules ```validate_full_key(key)``` - validates input key's all fields against the rules ```compare_keys(key_a, key_b)``` - Compares two keys against each other and the rules ```validate_tuple(tuple)``` - validates the tuple against the rules Fixes #4111
1 parent 28f1245 commit f17772b

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

doc/reference/reference_lua/key_def.rst

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,99 @@ to extract or compare the index key values.
221221
key_def = require('key_def')
222222
k = key_def.new({{type = 'string', fieldno = 3}})
223223
k:totable()
224+
225+
.. _key_validate_key:
226+
227+
.. method:: validate_key(key)
228+
229+
Since version :doc:`3.1.0 </release/3.1.0>`
230+
Validates whether the input ``key`` (partially or completely) matches the rules of the key definition object.
231+
Returns nothing on success.
232+
If the key fails the validation, a ``box.error`` type exception is raised.
233+
234+
**Example:**
235+
236+
.. code-block:: lua
237+
238+
local key_def = require('key_def')
239+
-- Create a rule: key = {id (number), name (string)}
240+
local rules = key_def.new({
241+
{fieldno = 1, type = 'number'},
242+
{fieldno = 2, type = 'string'}
243+
})
244+
-- Validate key {1001} (only id data type). Returns nothing:
245+
local ok, err = rules:validate_key({1001})
246+
247+
.. _key_validate_full_key:
248+
249+
.. method:: validate_full_key(key)
250+
251+
Since version :doc:`3.1.0 </release/3.1.0>`
252+
Validates whether they input ``key`` contains all fields and mathces the rules of the key definition object.
253+
Returns nothing on success.
254+
If the key fails the validation, a ``box.error`` type exception is raised.
255+
256+
**Example:**
257+
258+
.. code-block:: lua
259+
260+
local key_def = require('key_def')
261+
-- Create a rule: full key = {id, name}
262+
local rules = key_def.new({
263+
{fieldno = 1, type = 'number'},
264+
{fieldno = 2, type = 'string'}
265+
})
266+
-- Validate full key {1001, "Testuser"}. Returns nothing:
267+
local ok, err = rules:validate_full_key({1001, "Testuser"})
268+
269+
.. _key_validate_tuple:
270+
271+
.. method:: validate_tuple(tuple)
272+
273+
Since version :doc:`3.1.0 </release/3.1.0>`
274+
Validates whether the ``tuple`` matches the rules of the key definition object
275+
Returns nothing on success.
276+
If the key fails the validation, a ``box.error`` type exception is raised.
277+
278+
**Example:**
279+
280+
.. code-block:: lua
281+
282+
local key_def = require('key_def')
283+
-- Create a rule: tuple = {id (number), name (string), age (number)}
284+
local rules = key_def.new({
285+
{fieldno = 1, type = 'number'},
286+
{fieldno = 2, type = 'string'},
287+
{fieldno = 3, type = 'number'}
288+
})
289+
-- Validate tuple {1001, "Testuser", 28}. Returns nothing:
290+
local ok, err = rules:validate_tuple({1001, "Testuser", 28})
291+
292+
.. _key_compare_keys:
293+
294+
.. method:: compare_keys(key_a, key_b)
295+
296+
Since version :doc:`3.1.0 </release/3.1.0>`
297+
Compares two keys against each other and according to the key definition object.
298+
On success, returns:
299+
* ``<0`` if ``key_a`` parts are less than ``key_b`` parts
300+
* ``0`` if ``key_a`` parts are equal to ``key_b`` parts
301+
* ``>0`` if ``key_a`` parts are greater than ``key_b`` parts
302+
303+
If any key does not match the key definition rules, a ``box.error`` type exception is raised.
304+
305+
**Example:**
306+
307+
.. code-block:: lua
308+
309+
local key_def = require('key_def')
310+
-- Create rules: key = {timestamp (number), user_id (number)}
311+
local rules = key_def.new({
312+
{fieldno = 1, type = 'number'},
313+
{fieldno = 2, type = 'number'}
314+
})
315+
-- Compare keys. Returns -1
316+
local result = rules:compare_keys(
317+
{1748266198238, 1001}, -- 2025-05-26, user_id=1001
318+
{1748266198239, 1002} -- 2025-05-26, user_id=1002
319+
)

0 commit comments

Comments
 (0)