personLast updated on
Last updated on
Build and validate person information
Build and validate person information. The Person primitive captures individual name components and personal details.
Examples
Creating persons
// Object pattern
const person = open.person({
fullName: 'John Michael Smith'
})
// Builder pattern
const person = open.person()
.fullName('John Michael Smith')
.title('Dr.')
.firstName('John')
.middleName('Michael')
.lastName('Smith')
.suffix('Jr.')
.build()
// Copy and modify
const updated = open.person()
.from(person)
.title('Prof.')
.build()Parsing external data
// Parse unknown input (throws on error)
const person = open.person.parse(jsonData)
// Safe parsing (returns result object)
const result = open.person.safeParse(jsonData)
if (result.success) {
console.log(`Name: ${result.data.fullName}`)
} else {
console.error(result.error.message)
}Using with forms
const form = open.form({
name: 'registration',
fields: {
applicant: { type: 'person', label: 'Applicant Information' }
}
})
const filled = form.fill({
fields: {
applicant: open.person()
.fullName('Jane Doe')
.firstName('Jane')
.lastName('Doe')
.build()
}
})API
Object Pattern
open.person(input: Person): PersonPass a Person object for validation. Returns the validated Person or throws on error.
const person = open.person({ fullName: 'Jane Doe' })Builder Pattern
Chain methods to build a Person value incrementally:
All methods return PersonBuilder and are chainable.
open.person()
fullName: (value: string) => PersonBuilder
Set full display name
title: (value: string | undefined) => PersonBuilder
Set honorific (Mr., Dr., etc.)
firstName: (value: string | undefined) => PersonBuilder
Set given/first name
middleName: (value: string | undefined) => PersonBuilder
Set middle name(s)
lastName: (value: string | undefined) => PersonBuilder
Set family/last name
suffix: (value: string | undefined) => PersonBuilder
Set suffix (Jr., III, etc.)
from: (value: Person) => PersonBuilder
Initialize from existing Person
build: () => Person
Build and validate
Static Methods
parse: (input: unknown) => Person
Parse unknown input (throws on error)
safeParse: (input: unknown) => Result<Person>
Parse unknown input (returns result object)
Properties
fullName: string
Complete display name.
title: string?
Honorific or title (Mr., Mrs., Dr., Prof.).
firstName: string?
Given name or first name.
middleName: string?
Middle name(s).
lastName: string?
Family name or surname.
suffix: string?
Name suffix (Jr., Sr., III, PhD, Esq.).
Common Titles
| Title | Usage |
|---|---|
Mr. | Adult male |
Mrs. | Married female |
Ms. | Female (marital status neutral) |
Miss | Unmarried female |
Dr. | Doctor (medical or academic) |
Prof. | Professor |
Rev. | Religious leader |
Hon. | Judge or official |
Common Suffixes
| Suffix | Meaning |
|---|---|
Jr. | Junior (same name as father) |
Sr. | Senior |
II, III, IV | Generational |
PhD | Doctor of Philosophy |
MD | Medical Doctor |
Esq. | Esquire (attorney) |
CPA | Certified Public Accountant |
Validation
The Person primitive validates:
fullNamemust be a non-empty string- All other fields are optional
// Valid
open.person({ fullName: 'John Doe' })
open.person({
fullName: 'Dr. John Michael Smith Jr.',
title: 'Dr.',
firstName: 'John',
middleName: 'Michael',
lastName: 'Smith',
suffix: 'Jr.'
})
// Invalid - throws Error
open.person({})
// Error: Invalid Person: fullName is required
open.person({ fullName: '' })
// Error: Invalid Person: fullName cannot be emptyRelated
- Primitives Overview - All available primitives
- Organization - Organization information
- Person Field - Person field type