tarantool / tarantool/doc

schema: `unique_items` annotation

Open
#4,998 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
15
Forks
49
Avg merge
1d 13h
Merged PRs (30d)
3

Description

Since Tarantool 3.4 the experimental.config.utils.schema module has
the unique_items annotation taken into account by the <schema object>:validate() method.

The annotation can be assigned to a schema node that represents an array
of strings. An attempt to assign it to another schema node leads to an
error on the schema object creation (schema.new()).

The <schema object>:validate() method now raises an error if the
unique_items annotation is assigned and truthly, but the corresponding
value (an array) contains the same string two or more times.

The schema.set() constructor (creates an array with enumerated values
without duplicates) now assigns the unique_items annotation to the
created schema node instead of defining its own validate annotation.

The schema.set() constructor now allows a user to define the
validate annotation for the new schema node.

Example 1 (use unique_items):

local schema = require('experimental.config.utils.schema')

local s = schema.new('myschema', schema.array({
    items = schema.scalar({type = 'string'}),
    unique_items = true,
}))

s:validate({'foo', 'bar'})
-- OK

s:validate({'foo', 'foo'})
-- error: [myschema] Values should be unique, but "foo" appears at least
-- twice

Example 2 (use the schema.set() constructor):

local schema = require('experimental.config.utils.schema')

local s = schema.new('myschema', schema.set({'foo', 'bar'}))

s:validate({'foo', 'bar'})
-- OK

s:validate({'foo', 'foo'})
-- error: [myschema] Values should be unique, but "foo" appears at least
-- twice

Example 3 (use the validate annotation):

local fun = require('fun')
local schema = require('experimental.config.utils.schema')

local s = schema.new('myschema', schema.set({
    'router',
    'storage',
    'rebalancer',
}, {
    validate = function(data, w)
        local kv = fun.iter(data):map(function(x) return x, true end):tomap()
        if kv.rebalancer and not kv.storage then
            w.error('rebalancer needs storage role enabled')
        end
    end,
}))

s:validate({'storage', 'rebalancer'})
-- OK

s:validate({'rebalancer'})
-- error: [myschema] rebalancer needs storage role enabled

Example 4 (unique_items on an unsupported type):

local schema = require('experimental.config.utils.schema')

local s = schema.new('myschema', schema.array({
    items = schema.scalar({type = 'integer'}),
    unique_items = true,
}))
-- error: [myschema] "unique_items" requires an array of strings, got an
-- array of integer items

Requested by @Totktonada in https://github.com/tarantool/tarantool/commit/62c864dc2da8ff63bc63dd0050bc34020ed4aaa1.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the existing documentation for experimental.config.utils.schema and review the schema.new(), schema.array(), schema.set(), and validate() entry points. Document unique_items behavior, schema.set() validation options, and unsupported-type errors using the supplied examples; done means the Tarantool 3.4 behavior and examples are covered accurately.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.