Skip to content

Known Fields Plus Passthrough Extras

If you need an object with a few validated child fields but you still want to allow extra keys through unchanged, combine schema with additionalProperties: true:

javascript
const detailsSchema = createSchema({
  message: { type: 'string', required: true },
  fieldErrors: {
    type: 'object',
    values: { type: 'string', minLength: 1 },
    required: false
  }
})

const schema = createSchema({
  details: {
    type: 'object',
    schema: detailsSchema,
    additionalProperties: true
  }
})

That means:

  • known child fields are validated and normalized by detailsSchema
  • unknown child fields are preserved unchanged
  • transport export becomes properties plus additionalProperties: true

Worked example:

javascript
const metadataSchema = createSchema({
  metadata: {
    type: 'object',
    additionalProperties: true
  }
})

Valid input:

javascript
metadataSchema.patch({
  metadata: {
    theme: 'dark',
    flags: {
      beta: true
    }
  }
})

Result:

javascript
{
  validatedObject: {
    metadata: {
      theme: 'dark',
      flags: {
        beta: true
      }
    }
  },
  errors: {}
}

Invalid input:

javascript
metadataSchema.patch({
  metadata: ['not-an-object']
})

Result:

javascript
{
  validatedObject: {
    metadata: ['not-an-object']
  },
  errors: {
    metadata: {
      field: 'metadata',
      code: 'TYPE_CAST_FAILED',
      message: 'Value could not be cast to the required type.',
      params: {}
    }
  }
}

That is the intended contract: object-ness is enforced, but the inner bag is not owned by this library.

Dotted path options for nested fields

Because nested errors use dotted paths, the opt-out options do too.

Skip a whole nested field:

javascript
workspaceViewSchema.patch({
  workspace: {
    slug: 'x'
  }
}, {
  skipFields: ['workspace.slug']
})

Skip a specific nested validator:

javascript
workspaceViewSchema.patch({
  workspace: {
    slug: 'x'
  }
}, {
  skipParams: {
    'workspace.slug': ['minLength']
  }
})

This keeps the options model flat and consistent with the error map.

GPL-3.0-only