Skip to content

Adds Support for `withDefault` Factories

Compare
Choose a tag to compare
@losandes losandes released this 16 May 04:41
7651684

withDefault also accepts functions, and will execute them if they
are used as the value, so your default values can be factories
(i.e. to generate ids, calculate based on local scope, etc.)

const uuid = require('uuid/v4')
const { blueprint, optional } = require('@polyn/blueprint')
const UUID_REGEX = /^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i

const optionalValues = blueprint('optionalValues', {
  idOrNewId: optional(UUID_REGEX).withDefault(uuid) // will generate a new uuid as default
})