Skip to content

feat: schema field default, min/max, and pattern#211

Merged
careck merged 5 commits into
developmentfrom
feat/schema-field-improvements
Jun 8, 2026
Merged

feat: schema field default, min/max, and pattern#211
careck merged 5 commits into
developmentfrom
feat/schema-field-improvements

Conversation

@careck

@careck careck commented Jun 2, 2026

Copy link
Copy Markdown
Member

Summary

  • default_value: on any field — sets the initial value for new notes instead of zero-values (Rhai key: default_value since default is reserved in Rhai)
  • min: / max: on number fields — declarative range validation without writing a validate closure
  • pattern: on text/email fields — regex validation shortcut

Built-in validation runs before closure-based validate, and both can produce errors. Frontend inputs get HTML min/max/pattern attributes for immediate browser hints, plus server-side validation on blur via IPC.

Closes #122

Changes

  • FieldDefinition struct: 4 new fields (default_value, min_value, max_value, pattern)
  • default_fields(): respects script-specified defaults via dynamic_to_field_value()
  • validate_field() / validate_fields(): run built-in checks (min/max/pattern) before closure
  • FieldDefInfo Tauri bridge: carries minValue, maxValue, pattern, hasDefault to frontend
  • TS FieldDefinition: new optional fields
  • FieldEditor.tsx: HTML min/max/pattern attributes on inputs
  • useNoteForm.ts: triggers blur validation for fields with built-in constraints
  • i18n: validation error keys added to all 7 locales
  • 20 new tests covering defaults, range validation, pattern validation, and combined scenarios

Rhai script usage

schema("Product", #{
    fields: [
        #{ name: "price", type: "number", min: 0, max: 99999 },
        #{ name: "sku", type: "text", pattern: "^[A-Z]{2}-\\d{4}$" },
        #{ name: "status", type: "select", options: ["Draft", "Active"], default_value: "Draft" },
        #{ name: "stock", type: "number", default_value: 0, min: 0 },
    ]
});

Test plan

  • cargo test -p krillnotes-core — 699 tests pass (20 new)
  • cargo clippy -p krillnotes-core — clean
  • cargo fmt --check — clean
  • npx tsc --noEmit — clean
  • Manual: create a schema with min/max number field, verify inline error on blur
  • Manual: create a schema with pattern text field, verify regex validation
  • Manual: create a schema with default_value, verify new notes pre-populate

careck added 5 commits June 2, 2026 20:32
…ions

Schema fields now support three new declarative options that reduce
boilerplate in Rhai scripts:

- `default_value:` sets initial field values for new notes instead of
  zero-values
- `min:` / `max:` on number fields for range validation without a
  full validate closure
- `pattern:` on text/email fields for regex validation

Built-in validation runs before any closure-based validate, and
composes with it (both can produce errors). Frontend inputs get
HTML min/max/pattern attributes for immediate browser hints, plus
IPC-based validation on blur.

Closes #122
Update the scripting guide with the three new field definition options:
- Field definition table: new keys with examples
- Field types table: mention min/max and pattern on relevant types
- Section 6 restructured: built-in shortcuts first, then closures
Showcase all three new field options in a real schema:
- price: min 0, max 999999, default 0
- stock: min 0, default 0
- sku: pattern ^[A-Z]{2,4}-\d{3,6}$
- category: changed to select with default "Other"
Script authors can now provide a human-readable error message alongside
pattern regex, shown instead of the raw regex when validation fails:

  pattern_message: "SKU must be 2-4 letters, a dash, then 3-6 digits"

Falls back to "Value does not match pattern: ..." when absent.
@careck careck merged commit d77daaf into development Jun 8, 2026
2 checks passed
@careck careck deleted the feat/schema-field-improvements branch June 8, 2026 23:57
careck added a commit that referenced this pull request Jun 9, 2026
PR #211 — default_value, min/max, pattern validation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant