Skip to main content

Size constraint annotations for parameters

You can add size constraint annotations to parameters of functions, queries, and operations to specify size limits on the values passed. Supported parameter types are text and byte_array.

Available annotations

  • @size(n): The parameter must have exactly size n
  • @size(min, max): The parameter must have a size in the range min to max
  • @min_size(n): The parameter must have a minimum size of n
  • @max_size(n): The parameter must have a maximum size of n

Examples

// query only accepts pubkeys of size 32
query get_info(@size(32) pubkey: byte_array) { ... }

// status parameter cannot be empty
function print_status(@min_size(1) status: text) { ... }

// can't register a user with name > 50 characters long
operation register_user(@max_size(50) name) { ... }

Combining annotations

Use @min_size and @max_size together, or the @size(min, max) shorthand for a range:

query q(@size(8, 16) data: byte_array) { ... }
...
q(x'0123') // error, data too small
q(x'00112233445566778899AABBCCDDEEFF00') // error, data too large
q(x'01234567890AB') // ok

Validation

Parameter size constraint annotations are checked when the function, query, or operation is called. They are checked against default parameters at compile-time where possible.

Operations: struct<operation> behavior

If an operation parameter has a size constraint annotation, the operation's corresponding struct<operation> attribute behaves as though it were defined with an equivalent size constraint annotation. For example, for operation register_user(@max_size(50) name):

struct<register_user>('This name is not allowed because it has more than fifty characters!') // error