ZCore LogoZCore
How to

How to introspect dynamic schemas (/_schema)

Retrieve frontend form schemas automatically with role-based field pruning and action filtering.

Frontend developers and low-code form generators (like Retool, Appsmith, or JSON Schema UI builders) can query the dedicated /_schema endpoint on any BaseRouter to fetch active, role-pruned schema definitions.

1. Router Configuration (Zero-Config)

The /_schema endpoint is generated automatically on every BaseRouter. You can customize its sub-path via schema_path or disable it using exclude:

# routers.py
from zcore import BaseRouter, RouteKey
from .models import Task
from .schemas import TaskCreate, TaskUpdate, TaskResponse
from .services import TaskService

class TaskRouter(BaseRouter[TaskCreate, TaskUpdate]):
    model = Task
    create_schema = TaskCreate
    update_schema = TaskUpdate
    schema_out = TaskResponse
    service = TaskService
    prefix = "/tasks"

    # Optional: Customize sub-path (Default is "/_schema")
    # schema_path = "/_schema"

    # Optional: Disable schema endpoint if not needed
    # exclude = {RouteKey.SCHEMA}

router_instance = TaskRouter()

2. Request Schema via HTTP

Query the /_schema endpoint using GET. You can fetch all schemas at once or target a specific operational action using ?action=...:

# 1. Fetch all available schemas (create, update, view, lookup)
curl -X GET "http://localhost:8000/tasks/_schema"

# 2. Fetch specific Input Schema for creation forms
curl -X GET "http://localhost:8000/tasks/_schema?action=create"

# 3. Fetch specific Input Schema for update forms
curl -X GET "http://localhost:8000/tasks/_schema?action=update"

# 4. Fetch specific Output Schema for single/list view
curl -X GET "http://localhost:8000/tasks/_schema?action=view"

3. Real-World Response Structure

ZCore returns the full Pydantic V2 JSON Schema. Any restricted field defined in ctx.restricted_fields (or __private__ for guests) is automatically pruned from both properties and required arrays:

{
  "success": true,
  "message": "Success",
  "data": {
    "properties": {
      "id": { "format": "uuid", "title": "Id", "type": "string" },
      "title": { "title": "Title", "type": "string" },
      "is_completed": { "default": false, "title": "Is Completed", "type": "boolean" }
      // Sensitive fields like 'assignee_email' are pruned automatically!
    },
    "required": ["id", "title"],
    "title": "TaskResponse",
    "type": "object"
  },
  "meta": {
    "action": "view",
    "restricted_fields": ["tasks.view.assignee_email"]
  }
}

Action Parameter Resolution:

  • ?action=create: Resolves and returns the input schema (create_schema).
  • ?action=update: Resolves and returns the modification schema (update_schema).
  • ?action=view: Resolves and returns the output serialization schema (schema_out).
  • ?action=lookup: Resolves and returns the relational projection schema (lookup_schema).
  • Omitted action: Returns an object containing all defined schemas keyed by action name ({"create": {...}, "update": {...}, ...}).
  • Non-existent action: Triggers an immediate 400 ValidationError.

On this page