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

@synanetics/settings-resolver

v2.0.0

Published

Settings mixin for MoleculerJS

Downloads

21

Readme

@synanetics/settings-resolver

A utility function to resolve settings from a structured default settings JSON file. It can optionally accept overrides using a path to a JSON file, JSON string or object with a matching structure. It can also optionally apply a schema for validation of the resulting object.

Usage

const settingsResolver = require('@synanetics/settings-resolver');
// or
import settingsResolver from '@synanetics/settings-resolver';

Setting Validation

It should be noted that both default and override settings are validated against a common schema:

  • key is required and must be a string
  • value is required and must be a boolean, number or string
  • description is optional but must be a string if present
    • this property is for humans only and is ignored when building the output object

Loading Defaults Only

At a minimum it requires the full path to a valid default JSON file.

default.json

[
    {
        "key": "example",
        "value": "example value",
        "description": "Optional"
    }
]

code

const settings = settingsResolver(path.resolve('./default.json'));

/* settings would be
 * { example: 'example value' }
 */

Applying Overrides

Overrides can be in the form of a file, a string or an object.

Any override could extend the default structure but it would be more descriptive to maintain it as a default.

By File

Override file paths must be prefixed with file://

default.json

[
    {
        "key": "example",
        "value": "example value"
    }
]

override.json

[
    {
        "key": "example",
        "value": "overriding value"
    }
]

code

const settings = settingsResolver(
    path.resolve('./default.json'),
    `file://${path.resolve('./override.json')}`,
);

/* settings would be
 * { example: 'overriding value' }
 */

By String

A string must be valid JSON

default.json

[
    {
        "key": "example",
        "value": "example value"
    }
]

override

process.env.OVERRIDE = "[{\"key\":\"example\",\"value\":\"overriding value\"}]"

code

const settings = settingsResolver(
    path.resolve('./default.json'),
    process.env.OVERRIDE,
);

/* settings would be
 * { example: 'overriding value' }
 */

By Object

Simply provide a valid object

default.json

[
    {
        "key": "example",
        "value": "example value"
    }
]

override

const override = [
    {
        key: 'example',
        value: 'overriding value',
    }
];

code

const settings = settingsResolver(
    path.resolve('./default.json'),
    override,
);

/* settings would be
 * { example: 'overriding value' }
 */

Validating the Resulting Object

You can optionally supply a schema object by which the resulting object should be validated against. It uses the fastest-validator to do this (also used by Moleculer JS for param validation).

default.json

[
    {
        "key": "example",
        "value": 1
    }
]

override

const override = [
    {
        key: 'example',
        value: 'not a number',
    }
];

validation schema

const validationSchema = {
    example: { type: 'number', optional: false, empty: false }
};

code

const settings = settingsResolver(
    path.resolve('./default.json'),
    override,
    validationSchema,
);

// above would throw SettingsValidationError as override sets an expected number value to a string

Errors Handling

settingsResolver does nothing to prevent errors being raised by underlying operations (think a filesystem read error or invalid JSON syntax being provided).

When it fails to validate values (input settings, or validating output against a provided schema) it will raise a SettingsValidationError which is also exported by this package to assist in error handling.

SettingsValidationError has a data property which contains the array of validation errors encountered.