SDK

Primitive Reference

Last updated on

Build and validate primitive data types

Primitives are validated data types used throughout Paradoc. Object primitives accept plain objects and provide builders. Scalar primitives (date, datetime, time, percentage, and rating) accept one value and do not have builders. Every primitive provides parse() and safeParse() for validation.

Available Primitives

PrimitiveDescription
attachmentFile attachment
metadataArtifact metadata
signatureSignature value
addressPostal address
bboxBounding box
coordinateLat/lng point
dateDate (YYYY-MM-DD)
datetimeDate and time
durationISO 8601 duration
identificationID document
moneyMonetary amount with currency
organizationOrganization info
percentagePercentage value
personPerson name
phonePhone number (E.164)
ratingRating value
timeTime only

Usage Patterns

Object Pattern

Pass a plain object for immediate validation:

import { money } from '@paradoc/core'

// Validates and returns the Money object
const price = money({ amount: 99.99, currency: 'USD' })

// Throws if invalid
const invalid = money({ amount: 'not a number', currency: 'USD' })

Builder Pattern

Chain methods to construct incrementally:

import { money } from '@paradoc/core'

const price = money()
  .amount(99.99)
  .currency('USD')
  .build()

Static Methods

Parse unknown data from external sources:

import { money } from '@paradoc/core'

// Throws on error
const price = money.parse(jsonData)

// Returns result object
const result = money.safeParse(jsonData)
if (result.success) {
  console.log(result.data.amount)
} else {
  console.error(result.error)
}

Using with Forms

Primitives are commonly used when filling form data:

import { p, money, address } from '@paradoc/core'

const form = p.form({
  name: 'invoice',
  fields: {
    total: { type: 'money', label: 'Total Amount' },
    billingAddress: { type: 'address', label: 'Billing Address' }
  }
})

const filled = form.fill({
  fields: {
    total: money({ amount: 150.00, currency: 'USD' }),
    billingAddress: address()
      .line1('123 Main Street')
      .locality('New York')
      .region('NY')
      .postalCode('10001')
      .country('US')
      .build()
  }
})

On this page