shape

How to validate options with defaults#

Goal: accept a user options object, fill in any missing values, and reject anything of the wrong type.

Recipe#

Write the spec as the object of default values. Each literal is optional and its value is both the default and the required type.

TS

const { Shape } = require('shape')

const applyOptions = Shape({
  port:    8080,
  host:    'localhost',
  retries: 3,
  verbose: false,
})

applyOptions({ port: 9090, verbose: true })
// → { port: 9090, verbose: true, host: 'localhost', retries: 3 }
//   (injected defaults follow the keys you passed)

Go

s := shape.MustShape(map[string]any{
    "port":    8080,
    "host":    "localhost",
    "retries": 3,
    "verbose": false,
})

out, _ := s.Validate(map[string]any{"port": 9090, "verbose": true})

Notes#

Make some options required#

Mix literals (optional) and type markers (required) freely:

Shape({
  apiKey: String,   // required
  region: 'us-east', // optional, defaults to 'us-east'
})

See Require fields.