Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from docker/v1.1.2
Browse files Browse the repository at this point in the history
fix faulty regexp for config referencing
  • Loading branch information
Patrick Camacho committed Sep 15, 2016
2 parents 2e4ec57 + 6a857f5 commit c2e8a26
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bonito/pulpo",
"version": "1.1.1",
"version": "1.1.2",
"description": "Configuration mechanism",
"author": "Patrick Camacho <patrick.camacho@docker.com>",
"license": "MIT",
Expand Down
6 changes: 3 additions & 3 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ function getter(
value = rawValue(config, path);
break;
case 'string':
value = rawValue.replace(/(\$\{(.*)\})/gi, (
value = rawValue.replace(/(\$\{([^\}]*)\})/gi, (
match: string,
group1: string,
key: string
) => dotty.get(config, key));
) => config[key]);
break;
default:
value = rawValue;
Expand Down Expand Up @@ -120,7 +120,7 @@ export default class Schema {
flags
);

Object.defineProperty(config, path, {get: curriedGetter });
Object.defineProperty(config, path, {get: curriedGetter});
return config;
}, {});

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/self-referencing-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
"baz": {
"description": "top level value",
"type": "string"
},
"qux": {
"description": "multiple references",
"type": "string",
"default": "${foo.bar}/${baz}"
}
}
50 changes: 49 additions & 1 deletion test/integration/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,62 @@ describe('parsing nested schemas', () => {
});
});

describe('parsing functions', () => {
it('passes the config object in', () => {
const schema = new Schema(nestedSchema);
expect(schema.hydrate({
foo: {
bar: 'value for bar',
},
baz: (config) => config['foo.bar']
})).toEqual({
foo: {
bar: 'value for bar'
},
baz: 'value for bar'
})
});

it('passes the config object in to default values', () => {
const schema = new Schema({
server: {
port: {
description: 'description',
type: 'number',
default: 8888
},
origin: {
description: 'server origin',
type: 'string',
default: (config) => `localhost:${config['server.port']}`
}
},
foo: {
description: 'dummy',
type: 'string',
default: (config) => config['server.origin'],
}
});

expect(schema.hydrate({ server: { port: 3000 } })).toEqual({
server: {
port: 3000,
origin: 'localhost:3000'
},
foo: 'localhost:3000'
});
})
});

describe('parsing self referencing strings', () => {
it('handles self references', () => {
const schema = new Schema(selfReferencingSchema);
expect(schema.hydrate({ baz: 'testing' })).toEqual({
foo: {
bar: 'testing/foo'
},
baz: 'testing'
baz: 'testing',
qux: 'testing/foo/testing'
});
});
});

0 comments on commit c2e8a26

Please sign in to comment.