Skip to content

ResourcefulObjectType

The ResourcefulObjectType class provides validation for complex nested objects with typed properties. It supports OpenAPI composition patterns and property validation.

For complete API reference, see ResourcefulObjectType.

Basic Usage

typescript
import { ResourcefulObjectType, ResourcefulStringType, ResourcefulNumberType } from '@nhtio/lucid-resourceful/definitions'

const addressType = ResourcefulObjectType({
  properties: {
    street: ResourcefulStringType({ minLength: 1, maxLength: 100 }),
    city: ResourcefulStringType({ minLength: 1, maxLength: 50 }),
    zipCode: ResourcefulStringType({ minLength: 5, maxLength: 10 })
  },
  required: ['street', 'city'],
  additionalProperties: false
})

Configuration

  • properties: object - Object property definitions
  • required?: string[] - Required property names
  • additionalProperties?: boolean | ResourcefulDataType - Allow additional properties
  • minProperties?: number - Minimum property count
  • maxProperties?: number - Maximum property count

Common Patterns

typescript
class User extends compose(BaseModel, withResourceful({ name: 'User' })) {
  // Contact information
  @resourcefulColumn.object({
    type: ResourcefulObjectType({
      properties: {
        email: ResourcefulStringType({ minLength: 5, maxLength: 100 }),
        phone: ResourcefulStringType({ minLength: 10, maxLength: 15 })
      },
      required: ['email'],
      additionalProperties: false
    }),
    nullable: true
  })
  declare contactInfo: object | null

  // Preferences object
  @resourcefulColumn.object({
    type: ResourcefulObjectType({
      properties: {
        theme: ResourcefulStringType({ enum: ['light', 'dark'] }),
        notifications: ResourcefulBooleanType()
      },
      additionalProperties: true
    }),
    nullable: true
  })
  declare preferences: object | null
}

Composition Patterns

Supports OpenAPI composition with oneOf, allOf, anyOf, and not:

typescript
const flexibleType = ResourcefulObjectType({
  properties: {
    data: {
      oneOf: [
        ResourcefulStringType({ minLength: 1, maxLength: 100 }),
        ResourcefulNumberType({ minimum: 0, maximum: 1000, format: 'double' })
      ]
    }
  }
})

Best Practices

  • Define clear property structures
  • Use required arrays for mandatory fields
  • Consider additionalProperties carefully
  • Validate nested object schemas thoroughly