Otter
Recipes
Otter
Upstream Recipe Tested
Contact Manager with Validation Recipe
Desktop contact management application with input validation and file-backed persistence.
Architecture & Runtime Preview
Otter >=1.0.1The recipe defines a custom `Contact` type and combines `otter-official/text-utils` with structural string scanning to validate email format `@` and `.`. Valid entries are appended to a persistent list.
- 1validate-input: Perform email address structure and non-empty name assertions (otter-official/text-utils)
- 2persist-contacts: Save validated contact cards into JSON storage (otter-official/file-helpers)
Source Code (4 files)
{
"schemaVersion": 1,
"id": "otter-official/contact-manager",
"version": "1.0.0",
"title": "Contact Manager with Validation",
"summary": "Desktop contact management application with input validation and file-backed persistence.",
"target": "desktop",
"requiresOtter": ">=1.0.1",
"capabilities": ["files", "ui"],
"packs": [
{ "id": "otter-official/file-helpers", "version": "1.0.1" },
{ "id": "otter-official/text-utils", "version": "1.0.1" }
],
"configuration": [
{ "id": "companyBookName", "title": "Address Book Name", "type": "text", "default": "Team Directory", "description": "Header title for the contact directory" },
{ "id": "allowDuplicates", "title": "Allow Duplicate Emails", "type": "boolean", "default": false, "description": "Whether multiple contacts can share an email address" }
],
"steps": [
{
"id": "validate-input",
"kind": "pack-call",
"description": "Perform email address structure and non-empty name assertions",
"source": "otter-official/text-utils",
"inputs": [{ "name": "email", "type": "text" }],
"outputs": [{ "name": "isValid", "type": "boolean" }]
},
{
"id": "persist-contacts",
"kind": "pack-call",
"description": "Save validated contact cards into JSON storage",
"source": "otter-official/file-helpers",
"inputs": [{ "name": "contacts", "type": "list" }],
"outputs": [{ "name": "bytesWritten", "type": "number" }]
}
],
"generatedFiles": [
{ "path": "src/contacts.ot", "template": "templates/contacts.ot.tmpl" }
],
"tests": [
"tests/test.ot"
],
"explanations": {
"beginner": "This application stores your contact list with names, emails, and phone numbers. It checks that every email address looks valid before saving it to your computer.",
"intermediate": "The recipe defines a custom `Contact` type and combines `otter-official/text-utils` with structural string scanning to validate email format `@` and `.`. Valid entries are appended to a persistent list.",
"advanced": "Separates validation business logic from UI and storage. On Windows, the contact cards bind to the desktop WebView2 bridge. On macOS/Linux and headless CI, the validation and contact storage execute natively via standard IO."
}
}
# ==============================================================================
# Generated by Otter Application Recipe: contact-manager 1.0.0
# Target: Contact Manager Application with File Persistence & Validation
# ==============================================================================
use "${import:otter-official/file-helpers}"
use "${import:otter-official/text-utils}"
a Contact has
id
name
email
phone
.
to validateEmail emailAddress
if emailAddress is ""
return false
.
if not emailAddress contains "@"
return false
.
if not emailAddress contains "."
return false
.
split emailAddress by "@" into atParts
atCount is length of atParts
if atCount is not 2
return false
.
domain is last of atParts
split domain by "." into domainParts
dotCount is length of domainParts
if dotCount is at least 2
return true
.
return false
.
to createContact cid name email phone
if not validateEmail email
say "Error: Invalid email format for contact: "
say email
return gone
.
c is a Contact
id of c is cid
name of c is name
email of c is email
phone of c is phone
return c
.
to searchContacts contacts term
matches are empty
for each c in contacts
cName is name of c
if cName contains term
add c to matches
.
.
return matches
.
to editContactPhone contacts targetId newPhone
updated are empty
for each c in contacts
cid is id of c
if cid is targetId
phone of c is newPhone
.
add c to updated
.
return updated
.
to deleteContact contacts targetId
remaining are empty
for each c in contacts
cid is id of c
if cid is not targetId
add c to remaining
.
.
return remaining
.
to displayContacts contacts titleText
say "=== " plus titleText plus " ==="
countVal is length of contacts
if countVal is 0
say "(No contacts found)"
return false
.
for each c in contacts
cid is id of c
cName is name of c
cEmail is email of c
cPhone is phone of c
namePadded is padRight cName 18 " "
emailPadded is padRight cEmail 24 " "
lineParts are
"#"
cid
": "
namePadded
" | "
emailPadded
" | "
cPhone
.
join lineParts with "" into line
say line
.
return true
.
# ------------------------------------------------------------------------------
# Storage operations
# ------------------------------------------------------------------------------
to saveContactsToFile contacts filePath
convert contacts to json into rawJson
writeSafe filePath rawJson
return true
.
to loadContactsFromFile filePath
raw is readOrFallback filePath "[]"
loaded is a thing
convert raw from json into loaded
return loaded
.
# ------------------------------------------------------------------------------
# Main Application Execution
# ------------------------------------------------------------------------------
storeFile is "contacts_store.json"
# Load saved contacts or initialize default directory
directory is loadContactsFromFile storeFile
dirLen is length of directory
if dirLen is 0
say "Initializing new contact directory: ${config:companyBookName}"
c1 is createContact 1 "Ada Lovelace" "ada@analytical.org" "555-0101"
if c1 is not gone
add c1 to directory
.
c2 is createContact 2 "Charles Babbage" "charles@engines.org" "555-0102"
if c2 is not gone
add c2 to directory
.
c3 is createContact 3 "Alan Turing" "alan@bletchley.uk" "555-0103"
if c3 is not gone
add c3 to directory
.
saveContactsToFile directory storeFile
.
displayContacts directory "${config:companyBookName}"
say "Active contact count:"
say length of directory
# ==============================================================================
# End-to-End Test: Contact Manager Application
# Tests: Creation, validation, search, edit, delete, persistence, and recovery
# ==============================================================================
use "../../../../packs/otter-official/file-helpers/src/main.ot"
use "../../../../packs/otter-official/text-utils/src/main.ot"
a Contact has
id
name
email
phone
.
to validateEmail emailAddress
if emailAddress is ""
return false
.
if not emailAddress contains "@"
return false
.
if not emailAddress contains "."
return false
.
split emailAddress by "@" into atParts
atCount is length of atParts
if atCount is not 2
return false
.
domain is last of atParts
split domain by "." into domainParts
dotCount is length of domainParts
if dotCount is at least 2
return true
.
return false
.
to createContact cid name email phone
if not validateEmail email
say "Error: Invalid email format"
return gone
.
c is a Contact
id of c is cid
name of c is name
email of c is email
phone of c is phone
return c
.
to searchContacts contacts term
matches are empty
for each c in contacts
cName is name of c
if cName contains term
add c to matches
.
.
return matches
.
to editContactPhone contacts targetId newPhone
updated are empty
for each c in contacts
cid is id of c
if cid is targetId
phone of c is newPhone
.
add c to updated
.
return updated
.
to deleteContact contacts targetId
remaining are empty
for each c in contacts
cid is id of c
if cid is not targetId
add c to remaining
.
.
return remaining
.
to saveContactsToFile contacts filePath
convert contacts to json into rawJson
writeSafe filePath rawJson
return true
.
to loadContactsFromFile filePath
raw is readOrFallback filePath "[]"
loaded is a thing
convert raw from json into loaded
return loaded
.
# ------------------------------------------------------------------------------
# Test 1: Email Validation
# ------------------------------------------------------------------------------
if not validateEmail "ada@example.org"
say "FAIL: valid email rejected"
.
if validateEmail "invalid-email"
say "FAIL: invalid email accepted"
.
if validateEmail "missing@domain"
say "FAIL: email without dot accepted"
.
# ------------------------------------------------------------------------------
# Test 2: Contact Creation & Invalid Data Rejection
# ------------------------------------------------------------------------------
cGood is createContact 1 "Ada Lovelace" "ada@example.org" "555-0101"
if cGood is gone
say "FAIL: valid contact creation failed"
.
cBad is createContact 2 "Bad Email" "notanemail" "555-0102"
if cBad is not gone
say "FAIL: invalid email should yield gone"
.
# ------------------------------------------------------------------------------
# Test 3: Search, Edit, and Delete Operations
# ------------------------------------------------------------------------------
directory are empty
add cGood to directory
c2 is createContact 2 "Charles Babbage" "charles@engines.org" "555-0102"
add c2 to directory
c3 is createContact 3 "Alan Turing" "alan@bletchley.uk" "555-0103"
add c3 to directory
if length of directory is not 3
say "FAIL: expected 3 initial contacts"
.
# Search by substring
adaMatches is searchContacts directory "Ada"
if length of adaMatches is not 1
say "FAIL: search Ada expected 1 match"
.
# Edit phone
directory is editContactPhone directory 2 "555-9999"
charlesFound is searchContacts directory "Charles"
cCharles is first of charlesFound
if phone of cCharles is not "555-9999"
say "FAIL: phone update failed"
.
# Delete contact
directory is deleteContact directory 3
if length of directory is not 2
say "FAIL: expected 2 contacts after delete"
.
# ------------------------------------------------------------------------------
# Test 4: End-to-End Persistence and Session Restart
# ------------------------------------------------------------------------------
testStorePath is "test_contacts_session.json"
# Save session 1
saveContactsToFile directory testStorePath
# Simulate session restart by reading from disk in new scope
reloadedBook is loadContactsFromFile testStorePath
if length of reloadedBook is not 2
say "FAIL: expected 2 reloaded contacts after restart"
.
firstContact is first of reloadedBook
if name of firstContact is not "Ada Lovelace"
say "FAIL: reloaded contact name mismatch"
.
# Clean up test file
writeSafe testStorePath ""
say "Contact-manager tests passed."
# ==============================================================================
# Demo: Contact Manager Application
# ==============================================================================
use "../../../../packs/otter-official/file-helpers/src/main.ot"
use "../../../../packs/otter-official/text-utils/src/main.ot"
a Contact has
id
name
email
phone
.
to validateEmail emailAddress
if emailAddress is ""
return false
.
if not emailAddress contains "@"
return false
.
if not emailAddress contains "."
return false
.
split emailAddress by "@" into atParts
atCount is length of atParts
if atCount is not 2
return false
.
domain is last of atParts
split domain by "." into domainParts
dotCount is length of domainParts
if dotCount is at least 2
return true
.
return false
.
to createContact cid name email phone
if not validateEmail email
say "Error: Invalid email format for contact: "
say email
return gone
.
c is a Contact
id of c is cid
name of c is name
email of c is email
phone of c is phone
return c
.
to searchContacts contacts term
matches are empty
for each c in contacts
cName is name of c
if cName contains term
add c to matches
.
.
return matches
.
to editContactPhone contacts targetId newPhone
updated are empty
for each c in contacts
cid is id of c
if cid is targetId
phone of c is newPhone
.
add c to updated
.
return updated
.
to deleteContact contacts targetId
remaining are empty
for each c in contacts
cid is id of c
if cid is not targetId
add c to remaining
.
.
return remaining
.
to displayContacts contacts titleText
say "=== " plus titleText plus " ==="
countVal is length of contacts
if countVal is 0
say "(No contacts found)"
return false
.
for each c in contacts
cid is id of c
cName is name of c
cEmail is email of c
cPhone is phone of c
namePadded is padRight cName 18 " "
emailPadded is padRight cEmail 24 " "
lineParts are
"#"
cid
": "
namePadded
" | "
emailPadded
" | "
cPhone
.
join lineParts with "" into line
say line
.
return true
.
# 1. Initialize and add contacts
directory are empty
c1 is createContact 1 "Ada Lovelace" "ada@analytical.org" "555-0101"
add c1 to directory
c2 is createContact 2 "Charles Babbage" "charles@engines.org" "555-0102"
add c2 to directory
c3 is createContact 3 "Alan Turing" "alan@bletchley.uk" "555-0103"
add c3 to directory
displayContacts directory "All Contacts"
# 2. Search contacts
matches is searchContacts directory "Ada"
displayContacts matches "Search Results for 'Ada'"
# 3. Edit contact phone
directory is editContactPhone directory 2 "555-9999"
displayContacts directory "After Phone Update"
# 4. Delete contact
directory is deleteContact directory 3
displayContacts directory "After Deleting Contact #3"
say "Demo finished successfully."
Installation & Usage
otter recipe inspect otter-official/contact-manager
otter recipe apply otter-official/contact-manager --project ./my-app
This application stores your contact list with names, emails, and phone numbers. It checks that every email address looks valid before saving it to your computer.