airbridge-link-migrator-core
v0.2.0
Published
Core utility functions for validation and transformation
Maintainers
Readme
airbridge-link-migrator-core
Type-safe utility functions for validation and transformation.
Installation
npm install airbridge-link-migrator-coreUsage
import { required, optional, check, compute } from 'airbridge-link-migrator-core'
// Required value extraction with validation
const value = required.string(input) // throws if not string
const url = required.string.expression(input, /^https?:\/\//) // validates with regex
// Optional value extraction (returns undefined if invalid)
const maybeString = optional.string(input)
const maybeNumber = optional.number(input)
// Type checking
if (check.string(value)) {
// value is string
}
// Schema validation
const result = required.patchSchema({
name: input.name,
age: input.age,
}, {
'name': 'string',
'age?': 'number',
})
// Conditional computation
compute.defined(value, (v) => {
// v is guaranteed to be defined
console.log(v)
})API Reference
required
required.string(value)- Extract required stringrequired.number(value)- Extract required numberrequired.boolean(value)- Extract required booleanrequired.patchSchema(input, schema)- Validate and transform object with schema
optional
optional.string(value)- Extract optional stringoptional.number(value)- Extract optional numberoptional.boolean(value)- Extract optional booleanoptional.patchSchema(input, schema)- Optionally validate and transform object
check
check.string(value)- Type guard for stringcheck.number(value)- Type guard for numbercheck.boolean(value)- Type guard for booleancheck.object(value)- Type guard for objectcheck.array(value)- Type guard for array
compute
compute.defined(value, callback)- Execute callback if value is definedcompute.object(value, callback)- Execute callback if value is object
License
MIT
