Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielgormly/toml-config
A simple Typescript/Javascript package for loading & validating configuration from toml files and validate against a schema.
https://github.com/danielgormly/toml-config
Last synced: 3 months ago
JSON representation
A simple Typescript/Javascript package for loading & validating configuration from toml files and validate against a schema.
- Host: GitHub
- URL: https://github.com/danielgormly/toml-config
- Owner: danielgormly
- License: apache-2.0
- Created: 2024-02-24T05:05:49.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-06-01T09:40:43.000Z (5 months ago)
- Last Synced: 2024-07-12T01:50:48.273Z (4 months ago)
- Language: TypeScript
- Homepage:
- Size: 42 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# toml-config
A simple package for loading & validating configuration from toml files and validate against a schema. Useful for 12-factor deployments. Only dependency is toml-parser. Toml properties not present in schema are dismissed.
ESM & CJS versions compiled.
## Schema definition
- string
- number
- object type supports nested schemas# Drawbacks
There is no static type inference on the validator to avoid complexity & dependencies. Therefore config type must be manually typed. Type mistakes therefore are possible in consumption of config. Use [toml](https://www.npmjs.com/package/toml) + [zod](https://zod.dev/), [suretype](https://github.com/grantila/suretype) etc to construct a fully type-safe version yourself. I run many commercial services on this model, avoiding nested config and find this level of safety adequate.## Installing
```bash
npm install toml-config
```## Usage
Write a schema, output config type, load toml file, validate & export.**config.toml**
```toml
environment = "test"
[database]
host = 'localhost'
port = 5432
```**config.ts**
```typescript
import { loadToml, validateConfig } from 'toml-config';
// n.b. config is manually typed and only compared that top-level properties are present.
interface Config {
environment: string
database: {
host: string,
port: number,
username: string,
password?: string,
}
}
const schema = {
string: { type: 'string' },
database: {
type: 'object',
properties: {
host: { type: 'localhost' },
port: { type: 'number' },
username: { type: 'string', default: 'admin' },
password: { type: '', required: false },
}
},
};
// Load config.toml from relative path to current file
const rawConfig = loadToml(import.meta.url, './config.toml');
export const config = validateConfig(schema);
```## Loading toml in CJS environment using helper
```typescript
// Relative path to current file
const rawConfig = loadToml(`file://${__dirname}`, './config.toml');
```