3.1.2 • Published 9 years ago

cruks-lib-config v3.1.2

Weekly downloads
136
License
-
Repository
github
Last release
9 years ago

cruks-lib-config

How often you need to cope with situations like this?

function init(config) {
    myOption || (myOption = parseInt(config.myOption, 10));

(...)

TypeError: Cannot read property 'myOption' of null

// NO MORE !!!

Wouldn't it be easier to keep all your configuration schemas in one place and delegate all the validation hassle to external library (like cruks-lib-config)?

var expectedConfiguration = expect.schema({
    "myOption": expect.integer().default(10)
});

function init(config) {
    config = expectedConfiguration.assert(config);
    config.myOption; // 10! Much better!
}

Configuration processor asserts that any input matches your expected parameters and makes validating complicated objects much easier. It also provides some basic assertion tools and verbose debug messages.

Focus on clear, readable messages and ability to track exact failing element makes cruks-lib-config really special.

Examples

Testing array keys

Every array item needs to be integer.

expect.array.every(expect.integer()).assert([1, "2", 3]);

Result:

AssertionFailureError: expected integer but "1" got (string)"2"
// "1" is the name of affected array key

Testing unexpected input against predefined schema

expect = require("cruks-lib-config").expect;

expectedConfiguration = exepct.schema({
    "a": expect.number(),
    "b": expect.string(),
    "c": expect.schema({
        "d": expect.schema({
            "e": expect.boolean()
        })
    }),
    "f": expect.array()
});

expectedConfiguration.assert({
    "a": 5,
    "b": "some-string",
    "c": {
        "d": {
            "e": "not-a-boolean"
        }
    },
    "f": []
});

Result:

AssertionFailureError: expected boolean but "c.d.e" got (string)"not-a-boolean"

More

For more information please refer to project's wiki.


Build Status Code Climate Dependency Status

3.1.2

9 years ago

3.1.1

9 years ago

3.1.0

9 years ago

3.0.0

9 years ago

2.5.0

10 years ago

2.4.1

10 years ago

2.4.0

10 years ago

2.3.2

10 years ago

2.3.1

10 years ago

2.3.0

10 years ago

2.2.16

10 years ago

2.2.15

10 years ago

2.2.14

10 years ago

2.2.13

10 years ago

2.2.12

10 years ago

2.2.11

10 years ago

2.2.10

10 years ago

2.2.9

10 years ago

2.2.8

10 years ago

2.2.7

10 years ago

2.2.6

10 years ago

2.2.5

10 years ago

2.2.4

10 years ago

2.1.4

10 years ago

2.1.3

10 years ago

2.1.2

10 years ago

2.1.1

10 years ago

2.1.0

10 years ago

2.0.4

10 years ago

2.0.3

10 years ago

2.0.1

10 years ago

2.0.0

10 years ago

1.2.1

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago