Releases: losandes/polyn-blueprint
Fixes Blueprint Handling of Prototypes
When objects with a prototype are validated with blueprint, the output
values now implement that prototype.
Validates String Length for Type 'string'
While empty strings are of type string, that is rarely if ever a valid
input. This library is meant to keep validation simple, and to lean
towards common use, so it will prefer non-empty strings by default.
Adds TypeScript Definitions For Optionals
Adds Support for `withDefault` Factories
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
})
Adds `optional` and `optional.withDefault`
The optional
function allows you to make any validator, even function, and expression based validators, optional. It also supports default values, which are used in the even that the given inputs are null, or undefined. If an input is defined, and it doesn't pass validation, the default value is not used: an error will still be produced.
const { blueprint, gt, optional } = require('@polyn/blueprint')
const optionalValues = blueprint('optionalValues', {
stringOrDefault: optional('string').withDefault('foo'),
maybeGt20: optional(gt(20)),
gt20OrDefault: optional(gt(20)).withDefault(42),
maybeEnum: optional(/^book|magazine$/),
enumOrDefault: optional(/^book|magazine|product$/).withDefault('product'),
maybeCustom: optional(({ value }) => typeof value === 'string')
.withDefault('custom')
})
Fixes Error Messages For Nested Blueprints
Registered blueprint error messages were different than simply nesting an object in a blueprint: the property names weren't qualified. This fixes that so properties that are validated against a registered blueprint have qualified error messages (i.e. instead of "expected id
{null} to be {string}", we see "expected user.id
{null} to be {string}").
NPM Publishing Cleanup
- Removes unnecessary files from npm
- Updates README with more examples, and a TOC
Guards The Args For Default Validation Messages
Removes the possibility of a null pointer exception in the default error messages
Guards The Args For Default Validation Messages
Removes the possibility of a null pointer exception in the default error messages
Improves Validation Messages
Makes validation messages more consistent, and concise. Also adds more context about
what was expected, and what was encountered.