3.2.3 • Published 10 months ago
@m234/config v3.2.3
@m234/config
Node.js config library for command-line tools with strict type check. Uses yaml format.
Features
- Validates the config types when loading: any, string, record, struct, integer, number, boolean.
- Each type has options. For examlple there is the 'pattern' option for strings and numbers.
- Struct type has dynamic properties validation ability.
Install
npm i @m234/configUsage
import {homedir} from "node:os"
import {exit} from "node:process"
import {join} from "node:path"
import {Config, Types} from "@m234/config"
function exitFail(message: string | undefined) {
console.error(message)
process.exit(1)
}
const aORb = Types.literal({choices: new Set(['a', 'b'])})
const cfg = new Config({
path: join(homedir(), 'app.yaml'), // or use `find-config` package
type: Types.struct({
properties:{
id: Types.integer({min: 0})
// min 8 chars password
password: Types.string({pattern: /.{8,}/})
records: Types.array({
elementType: aORb
})
}
})
})
exitFail(cfg.failLoad())
console.log(cfg.get('id') === 0)
console.log(cfg.getPrintable())