Resource Fields
Learn how to define fields for your api.vision resources, including types, validations, and special features.
Field Basics
Each resource in api.vision consists of a collection of fields. Fields define the structure and constraints of your data.
Field Definition
A typical field definition includes:
{
"resources": {
"users": {
"fields": {
"id": { "type": "id" },
"username": {
"type": "string",
"required": true,
"unique": true,
"minLength": 3,
"maxLength": 20
},
"email": {
"type": "string",
"format": "email",
"required": true
},
"age": {
"type": "integer",
"minimum": 13,
"default": 18
},
"role": {
"type": "string",
"enum": ["user", "admin", "editor"],
"default": "user"
},
"createdAt": {
"type": "datetime",
"readOnly": true
}
}
}
}
}
Field Types
api.vision supports a wide range of field types:
Type | Description | Example |
---|---|---|
id | Primary identifier for the resource | "id": { "type": "id" } |
string | Text value | "name": { "type": "string" } |
integer | Whole number | "age": { "type": "integer" } |
number | Floating-point number | "price": { "type": "number" } |
boolean | True/false value | "active": { "type": "boolean" } |
date | Date only (without time) | "birthDate": { "type": "date" } |
datetime | Date and time | "createdAt": { "type": "datetime" } |
time | Time only (without date) | "openingTime": { "type": "time" } |
array | List of values | "tags": { "type": "array", "items": { "type": "string" } } |
object | Nested object | "address": { "type": "object", "properties": { "street": { "type": "string" } } } |
relation | Relationship to another resource | "author": { "type": "relation", "resource": "users" } |
file | File reference | "avatar": { "type": "file" } |
uuid | UUID string | "externalId": { "type": "uuid" } |
enum | Value from a predefined set | "status": { "type": "string", "enum": ["active", "inactive"] } |
Field Attributes
Fields can have various attributes to define their behavior:
Attribute | Description | Example |
---|---|---|
required | Whether the field is required | "required": true |
default | Default value if none is provided | "default": "user" |
unique | Whether the field must be unique across all resources | "unique": true |
readOnly | Whether the field can only be read, not written | "readOnly": true |
writeOnly | Whether the field can only be written, not read (like passwords) | "writeOnly": true |
nullable | Whether the field can be null | "nullable": true |
description | Human-readable description of the field | "description": "User's email address" |
example | Example value for the field | "example": "john@example.com" |
Type-Specific Validations
Different field types support specific validation attributes:
String Validations
Attribute | Description | Example |
---|---|---|
minLength | Minimum string length | "minLength": 3 |
maxLength | Maximum string length | "maxLength": 20 |
pattern | Regular expression pattern | "pattern": "^[a-z0-9]+$" |
format | Predefined format validation | "format": "email" |
enum | List of allowed values | "enum": ["admin", "user", "guest"] |
Number and Integer Validations
Attribute | Description | Example |
---|---|---|
minimum | Minimum value | "minimum": 0 |
maximum | Maximum value | "maximum": 100 |
exclusiveMinimum | Value must be greater than (not equal to) minimum | "exclusiveMinimum": true |
exclusiveMaximum | Value must be less than (not equal to) maximum | "exclusiveMaximum": true |
multipleOf | Value must be a multiple of this number | "multipleOf": 5 |
Array Validations
Attribute | Description | Example |
---|---|---|
items | Schema for array items | "items": { "type": "string" } |
minItems | Minimum number of items | "minItems": 1 |
maxItems | Maximum number of items | "maxItems": 10 |
uniqueItems | Whether items must be unique | "uniqueItems": true |
Object Validations
Attribute | Description | Example |
---|---|---|
properties | Schema for object properties | "properties": { "street": { "type": "string" } } |
required | List of required properties | "required": ["street", "city"] |
additionalProperties | Whether additional properties are allowed | "additionalProperties": false |
Special Field Features
Computed Fields
Define fields that are calculated based on other fields:
"fullName": {
"type": "string",
"computed": true,
"expression": "firstName + ' ' + lastName"
}
Auto-Generated Values
Configure fields to be automatically generated:
"slug": {
"type": "string",
"generator": "slug",
"source": "title"
}
"createdAt": {
"type": "datetime",
"generator": "timestamp",
"readOnly": true
}
"uuid": {
"type": "uuid",
"generator": "uuid"
}
Custom Validators
Add custom validation logic:
"password": {
"type": "string",
"writeOnly": true,
"validator": {
"name": "password",
"options": {
"minLength": 8,
"requireNumbers": true,
"requireSpecialChars": true
}
}
}
Field Transformers
Automatically transform field values:
"email": {
"type": "string",
"format": "email",
"transform": "lowercase"
}
"title": {
"type": "string",
"transform": "trim"
}
Comprehensive Example
Here's a more complex example showing various field features:
{
"resources": {
"products": {
"fields": {
"id": {
"type": "id"
},
"sku": {
"type": "string",
"required": true,
"unique": true,
"pattern": "^[A-Z]{2}\d{6}$",
"description": "Product SKU in format XX000000"
},
"name": {
"type": "string",
"required": true,
"minLength": 3,
"maxLength": 100
},
"slug": {
"type": "string",
"generator": "slug",
"source": "name",
"unique": true
},
"description": {
"type": "string",
"nullable": true
},
"price": {
"type": "number",
"required": true,
"minimum": 0,
"exclusiveMinimum": true
},
"images": {
"type": "array",
"items": { "type": "string", "format": "uri" },
"minItems": 1,
"maxItems": 10
},
"categories": {
"type": "relation",
"resource": "categories",
"relation": "belongsToMany"
},
"specs": {
"type": "object",
"properties": {
"weight": { "type": "number" },
"dimensions": {
"type": "object",
"properties": {
"width": { "type": "number" },
"height": { "type": "number" },
"depth": { "type": "number" }
}
},
"color": { "type": "string" }
}
},
"inStock": {
"type": "boolean",
"default": true
},
"stockQuantity": {
"type": "integer",
"minimum": 0,
"default": 0
},
"status": {
"type": "string",
"enum": ["draft", "published", "archived"],
"default": "draft"
},
"tags": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true
},
"createdAt": {
"type": "datetime",
"readOnly": true,
"generator": "timestamp"
},
"updatedAt": {
"type": "datetime",
"readOnly": true,
"generator": "timestamp",
"onUpdate": true
}
}
}
}
}
Best Practices
- Define clear, consistent field types across related resources
- Use appropriate validations to ensure data integrity
- Add descriptions to fields to improve documentation
- Use computed fields for values that can be derived
- Apply sensible defaults where appropriate
- Mark sensitive fields as writeOnly to prevent exposure
- Use field transformers to ensure consistent data formatting