0.0.9 • Published 9 months ago

schematized-config v0.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

schematized-config

a library for loading configurations from .env, JSON, or plain JS/TS objects, validated or hydrated against a JSON schema with or without defaults.

usage

The main class is ValidatedConfig. Assuming a .env file with the following:

DATABASE_NAME=my-db
import { ValidatedConfig, ValidationStrictness } from 'schematized-config'


interface MySchema {
    FOO: string,
    BLUE?: number,
    URL?: string,
    DATABASE_NAME: string,
}

const config = ValidatedConfig.setSchema({
    type: 'object',
    properties: {
        FOO: {
            type: 'string',
            default: 'bar'
        },
        BLUE: {
            type: 'number',
        },
        URL: {
            type: 'string',
        },
        DATABASE_NAME: {
            type: 'string',
        },
    },
    required: [
        'FOO',
        'DATABASE_NAME',
    ]
}).load<MySchema>()

console.log(config)
// > { FOO: 'bar', BLUE: undefined, URL: undefined, DATABASE_NAME: 'my-db' }

By default, ValidatedConfig will attempt to read a configuration from .env using the dotenv library. The structure is validated against the JSON schema passed to setSchema. In this case, FOO and DATABASE_NAME are required entries, so FOO receives the schema default of bar; if no DATABASE_NAME was set in .env, the validation throws a validation error via ajv.

That's it. In practice, it is cumbersome to have to define a JSON schema and its interface at the same time. To automate this process, consider using json-schema-to-typescript to auto-generate typescript interfaces from your schemas. For composability across schemas, consider using jsonnet to generate and maintain your schemas.

0.0.9

9 months ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago