Skip to content

Commit

Permalink
Various minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 2, 2019
1 parent 373afb9 commit c6e805f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 75 deletions.
74 changes: 40 additions & 34 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
/// <reference types="node"/>
import {JSONSchema} from 'json-schema-typed';

declare namespace Conf {
interface Options<T> {
/**
Config used if there are no existing config.
**Note:** The values in `defaults` will overwrite the `default` key in the `schema` option.
*/
readonly defaults?: {[key: string]: T};

/**
[JSON Schema](https://json-schema.org) to validate your config data.
Under the hood, the JSON Schema validator [ajv](https://github.com/epoberezkin/ajv) is used to validate your config. We use [JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html) and support all [validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md) and [formats](https://github.com/epoberezkin/ajv#formats).
You should define your schema as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property. See more [here](https://json-schema.org/understanding-json-schema/reference/object.html#properties).
@example
```
import Conf = require('conf');
const schema = {
foo: {
type: 'number',
maximum: 100,
minimum: 1,
default: 50
},
bar: {
type: 'string',
format: 'url'
}
};
const config = new Conf({schema});
console.log(config.get('foo'));
//=> 50
config.set('foo', '1');
// [Error: Config schema violation: `foo` should be number]
```
**Note:** The `default` value will be overwritten by `defaults` option if set.
*/
readonly schema?: {[key: string]: JSONSchema};

/**
Name of the config file (without extension).
Expand Down Expand Up @@ -85,40 +125,6 @@ declare namespace Conf {
@default 'nodejs'
*/
readonly projectSuffix?: string;

/**
[JSON Schema](https://json-schema.org) to validate your config data.
Under the hood, the JSON Schema validator [ajv](https://github.com/epoberezkin/ajv) is used to validate your config. We use [JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html) and support all [validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md) and [formats](https://github.com/epoberezkin/ajv#formats).
You should define your schema as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property. See more [here](https://json-schema.org/understanding-json-schema/reference/object.html#properties).
@example
```
import Conf = require('conf');
const schema = {
foo: {
type: 'number',
maximum: 100,
minimum: 1,
default: 50
},
bar: {
type: 'string',
format: 'url'
}
};
const config = new Conf({schema});
config.set('foo', '1');
// [Error: Config schema violation: `foo` should be number]
```
__Please note the `default` value will be overwritten by `defaults` option if set.__
*/
readonly schema?: {[key: string]: unknown};
}
}

Expand Down
4 changes: 3 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import {expectType, expectError} from 'tsd';
import Conf = require('.');

const conf = new Conf<string | number | boolean>();
Expand All @@ -20,7 +20,9 @@ new Conf<string>({clearInvalidConfig: false});
new Conf<string>({serialize: value => 'foo'});
new Conf<string>({deserialize: string => ({})});
new Conf<string>({projectSuffix: 'foo'});

new Conf<string>({schema: {foo: {type: 'string'}}});
expectError(new Conf<string>({schema: {foo: {type: 'nope'}}}));

conf.set('foo', 'bar');
conf.set('hello', 1);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@
"ajv": "^6.10.0",
"dot-prop": "^4.2.0",
"env-paths": "^2.1.0",
"make-dir": "^2.1.0",
"json-schema-typed": "^7.0.0",
"make-dir": "^3.0.0",
"pkg-up": "^3.0.1",
"write-file-atomic": "^2.4.2"
},
"devDependencies": {
"@types/node": "^11.12.2",
"ava": "^1.4.0",
"clear-module": "^3.1.0",
"del": "^4.0.0",
"del": "^4.1.0",
"tempy": "^0.2.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
Expand Down
78 changes: 40 additions & 38 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,46 @@ Type: `Object`

Config used if there are no existing config.

**Please note the values in `defaults` will overwrite the `default` key in `schema` option.**
**Note:** The values in `defaults` will overwrite the `default` key in the `schema` option.

#### schema

type: `Object`

[JSON Schema](https://json-schema.org) to validate your config data.

Under the hood, the JSON Schema validator [ajv](https://github.com/epoberezkin/ajv) is used to validate your config. We use [JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html) and support all [validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md) and [formats](https://github.com/epoberezkin/ajv#formats).

You should define your schema as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property. See more [here](https://json-schema.org/understanding-json-schema/reference/object.html#properties).

Example:

```js
const Conf = require('conf');

const schema = {
foo: {
type: 'number',
maximum: 100,
minimum: 1,
default: 50
},
bar: {
type: 'string',
format: 'url'
}
};

const config = new Conf({schema});

console.log(config.get('foo'));
//=> 50

config.set('foo', '1');
// [Error: Config schema violation: `foo` should be number]
```

**Note:** The `default` value will be overwritten by `defaults` option if set.

#### configName

Expand Down Expand Up @@ -149,43 +188,6 @@ You can pass an empty string to remove the suffix.

For example, on macOS, the config file will be stored in the `~/Library/Preferences/foo-nodejs` directory, where `foo` is the `projectName`.

#### schema

type: `Object`<br>
Default: `undefined`

[JSON Schema](https://json-schema.org) to validate your config data.

Under the hood, JSON Schema validator [ajv](https://github.com/epoberezkin/ajv) is used to validate your config. We use [JSON Schema draft-07](http://json-schema.org/latest/json-schema-validation.html) and support all [validation keywords](https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md) and [formats](https://github.com/epoberezkin/ajv#formats).

You should define your schema as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property. See more [here](https://json-schema.org/understanding-json-schema/reference/object.html#properties).

Example:

```js
const Conf = require('conf');

const schema = {
foo: {
type: 'number',
maximum: 100,
minimum: 1,
default: 50
},
bar: {
type: 'string',
format: 'url'
}
};

const config = new Conf({schema});

config.set('foo', '1');
// [Error: Config schema violation: `foo` should be number]
```

**Please note the `default` value will be overwritten by `defaults` option if set.**

### Instance

You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a `key` to access nested properties.
Expand Down

0 comments on commit c6e805f

Please sign in to comment.