> ## Documentation Index
> Fetch the complete documentation index at: https://docs.standardcomputers.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Constraints: Defining and Configuring Fields in a Standard

> Learn the full syntax for Standard constraints including access types, data types, length validation, references, defaults, and required fields.

Constraints are the building blocks of every Standard. Each constraint defines a single field in a Standard — specifying how it can be accessed, what type of value it holds, how long that value can be, and whether it is required. Understanding the full constraint syntax lets you model your data precisely and enforce rules at the schema level.

## Constraint Syntax

Every constraint follows this format:

```stds theme={null}
[ACCESS_TYPE] CONST_NAME [CONST_TYPE [:LENGTH[!]]] [NULL] [CONST_REF] ["DEFAULT_VALUE" | "/REGEX/"] [*]
```

The sections below describe each part of the syntax in detail.

### ACCESS\_TYPE

The access type controls the scope in which a constraint can be read or written.

<ParamField path="private">
  Only accessible in the constructor when initially setting the value. Once a record is created, this value cannot be changed.
</ParamField>

<ParamField path="protected">
  Can be publicly read but only written to in the constructor. Use `protected` for fields that should be visible everywhere but set only at creation time.
</ParamField>

<ParamField path="public">
  Allows read and write access from any point or user within the package. Use `public` for fields that need to be freely updated after record creation.
</ParamField>

<ParamField path="global">
  Can be read from anywhere but only manipulated from within the package. Use `global` for system-level fields that external callers should see but not modify.
</ParamField>

### CONST\_NAME

<ParamField path="CONST_NAME" type="string" required>
  The name of the constraint. Must contain only lowercase letters, numbers, or underscores. For example: `vin`, `make`, `max_weight`.
</ParamField>

### CONST\_TYPE

<ParamField path="CONST_TYPE" type="string">
  The data type of the constraint value. Supported types are:

  | Type       | Description                            |
  | ---------- | -------------------------------------- |
  | `string`   | A sequence of characters               |
  | `bool`     | A boolean (`true` or `false`)          |
  | `int`      | A whole number integer                 |
  | `double`   | A floating-point number                |
  | `char`     | A single character                     |
  | `array`    | An ordered list of values              |
  | `Standard` | A reference to another Standard record |
</ParamField>

### LENGTH

<ParamField path="LENGTH" type="integer">
  The number of characters accepted, expressed as an integer. You can optionally append `!` to enforce an exact length.

  * `string:36` — the value can be **up to** 36 characters.
  * `string:36!` — the value must be **exactly** 36 characters.

  Omitting `LENGTH` entirely places no character limit on the constraint.
</ParamField>

### CONST\_REF

<ParamField path="CONST_REF" type="string">
  A shorthand reference for the constraint that enables quick access in queries. For example, if the Standard `school` has reference `SCH` and its `street` constraint has reference `SADDR`, you can access that field as `SCH.SADDR` anywhere in your system.
</ParamField>

### DEFAULT\_VALUE or REGEX

<ParamField path="&#x22;DEFAULT_VALUE&#x22; | &#x22;/REGEX/&#x22;">
  Provide a default value in double quotes to use when no value is supplied for this constraint. If no default is set and no value is provided, the saved value will be `NULL`.

  Alternatively, provide a regex pattern (also in quotes, surrounded by `/` delimiters) to require that any submitted value matches the pattern before the record is saved.
</ParamField>

### Required Marker (`*`)

<ParamField path="*">
  When a constraint ends with `*`, a value must be provided. If the calling code does not supply a value for that constraint, the record will be rejected entirely.
</ParamField>

## Full Example

The following Standard demonstrates all constraint options together:

```stds theme={null}
vehicle: VHL {
    private vin string:36! VN *
    protected make string MK *
    protected model string MDL *
    protected year int YR "2024"
    protected color string:20 CLR
}
```

* `vin` is private, must be exactly 36 characters, and is required.
* `make` and `model` are protected and required, with no length limit.
* `year` is protected with a default value of `2024` — if no year is supplied, the record saves `2024` automatically.
* `color` is protected, accepts up to 20 characters, and is optional.

<Warning>
  Omitting `*` on a constraint makes it optional. If you also omit a default value, the record will store `NULL` for that field when no value is provided. Make sure `NULL` is an acceptable state for optional constraints in your application logic.
</Warning>
