npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

toml-config

v1.1.1

Published

A simple Typescript/Javascript package for loading & validating configuration from toml files and validate against a schema.

Downloads

43

Readme

toml-config

CI

A simple, package for loading & validating toml files and validating against a schema, with static type inference so the resulting config item is typed. Useful for configuration files for app deployments in 12-factor style. Only dependency is toml. Properties not present in schema are discarded.

Both ESM & CJS versions published.

Schema definition

  • string
  • number
  • object type supports nested schemas

Installing

npm install toml-config

Usage

Write a schema, output config type, load toml file, validate & export.

config.toml

environment = "dev"
email = "[email protected]"
[database]
host = 'localhost'
port = 5432

config.ts

import { loadToml, validateConfig } from 'toml-config';
const schema = {
  environment: { type: 'string' },
  email: { type: 'string', format: 'email' },
  database: {
    type: 'object',
    properties: {
      host: { type: 'string' },
      port: { type: 'number' },
      username: { type: 'string', default: 'admin' },
      password: { type: 'string', required: false },
    }
  },
};
// Load config.toml from relative path to current file
const rawConfig = loadToml(import.meta.url, './config.toml');
export const config = validateConfig(schema, rawConfig);

String format validation

The 'string' schema option optionally takes a format attribute. The following formats are allowed: email: valid email (n.b. see src/regex.ts) http: http or https url https: https only url url: qualified urls with a tld (e.g. example.com, n.b. localhost will not pass)

Loading toml in CJS environment using helper

// Relative path to current file
const rawConfig = loadToml(`file://${__dirname}`, './config.toml');

Inferring a type from the schema separately

You may want to do this to include the schema in functions later.

import { Schema, InferConfig } from 'toml-config';
const schema = {
  foo: { type: "number" },
  bar: { type: "string" },
} satisfies Schema;

type Config = InferConfig<typeof schema>;

const config = {
  foo: 6,
  bar: "bo",
};

function func(config: Config) { // satisfies
  console.log(config);
}
func(config);