Skip to content

Commit

Permalink
fix: update README and prettier the code
Browse files Browse the repository at this point in the history
  • Loading branch information
sshah-asymmetrik committed Jan 28, 2019
1 parent cee9a8d commit a7eb6f1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
7 changes: 4 additions & 3 deletions packages/fhir-sanitize-param/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ Is this parameter required.
Type: `String`
Required: `false`

### Valid Types
**NOTE:** Arguments present in the request that have not been specified in the array of allowed arguments will be passed through un-sanitized under a separate key `args._raw`. You will need to perform your own validation on this afterwards.


Allowed types are currently `number`, `date`, `boolean`, `string`, `token`, and `json_string`.
### Valid Types

**NOTE:** `json_string` expects stringified JSON and will attempt to call `JSON.parse` on it. The resulting object will be passed through as the sanitized argument. You will need to perform your own validation on this afterwards.
Allowed types are currently `number`, `date`, `uri` `reference`, `string`, `token`, `quantity` and `boolean`.
19 changes: 11 additions & 8 deletions packages/fhir-sanitize-param/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function splitPrefixFromValue(inputValue) {

return {
prefix: prefix,
value: value
value: value,
};
}

Expand All @@ -137,9 +137,9 @@ function sanitizeBoolean(name, inputValue, type = 'boolean') {
* @returns {*}
*/
function sanitizeDate(name, inputValue, type = 'date') {
let {prefix, value} = splitPrefixFromValue(inputValue);
let { prefix, value } = splitPrefixFromValue(inputValue);
invariant(moment(value).isValid(), mismatchError(type, name));
return (prefix + value);
return prefix + value;
}

/**
Expand All @@ -151,17 +151,20 @@ function sanitizeDate(name, inputValue, type = 'date') {
* @returns {*}
*/
function sanitizeNumber(name, inputValue, type = 'number') {
let {prefix, value} = splitPrefixFromValue(inputValue);
let { prefix, value } = splitPrefixFromValue(inputValue);
const coercedVal = validator.toFloat('' + value);
invariant(typeof coercedVal === 'number' && !Number.isNaN(coercedVal), mismatchError(type, name));
invariant(
typeof coercedVal === 'number' && !Number.isNaN(coercedVal),
mismatchError(type, name),
);
const expectedval = Number(coercedVal);
const givenval = Number(value);

invariant(
expectedval === givenval,
`Expected value: ${expectedval} does not equal given value: ${givenval}`,
);
return (prefix + '' + value);
return prefix + '' + value;
}

/**
Expand Down Expand Up @@ -217,14 +220,14 @@ function sanitizeQuantity(name, inputValue, type = 'quantity') {
let system = '';
let code = '';
if (token) {
({system, code} = sanitizeToken(name, token, 'quantity.token'));
({ system, code } = sanitizeToken(name, token, 'quantity.token'));
}
invariant(number, mismatchError(type, name));

return {
number: number,
system: system,
code: code
code: code,
};
}

Expand Down
68 changes: 42 additions & 26 deletions packages/fhir-sanitize-param/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ describe('Parameter Sanitization Test', () => {
method: 'GET',
query: {
foo: 'foo',
bar: 'bar'
bar: 'bar',
},
};
let configs = [{ name: 'foo', type: 'string' }, { name: 'bar', type: 'whoknows' }];
let configs = [
{ name: 'foo', type: 'string' },
{ name: 'bar', type: 'whoknows' },
];
let { errors, args } = sanitizer(request, configs);
expect(errors).toHaveLength(1);
expect(errors[0].message).toContain('Unsupported type whoknows for parameter bar');
expect(errors[0].message).toContain(
'Unsupported type whoknows for parameter bar',
);
expect(args._raw.bar === 'bar');
expect(args.foo[0].value === 'foo');
});
Expand Down Expand Up @@ -112,7 +117,7 @@ describe('Parameter Sanitization Test', () => {
});

describe('number', () => {
test('Should implicitly apply \'eq\' prefix', () => {
test("Should implicitly apply 'eq' prefix", () => {
let request = {
method: 'GET',
query: {
Expand Down Expand Up @@ -174,7 +179,9 @@ describe('Parameter Sanitization Test', () => {
let configs = [{ name: 'foo', type: 'number' }];
let { errors, args } = sanitizer(request, configs);
expect(errors).toHaveLength(1);
expect(errors[0].message).toContain('expected number for parameter foo');
expect(errors[0].message).toContain(
'expected number for parameter foo',
);
expect(args.foo).toBeUndefined();
});

Expand All @@ -188,7 +195,9 @@ describe('Parameter Sanitization Test', () => {
let configs = [{ name: 'foo', type: 'number' }];
let { errors, args } = sanitizer(request, configs);
expect(errors).toHaveLength(1);
expect(errors[0].message).toContain('Expected value: 3.14159 does not equal given value: NaN');
expect(errors[0].message).toContain(
'Expected value: 3.14159 does not equal given value: NaN',
);
expect(args.foo).toBeUndefined();
});

Expand Down Expand Up @@ -359,10 +368,10 @@ describe('Parameter Sanitization Test', () => {
let request = {
method: 'GET',
query: {
foo: '12'
}
foo: '12',
},
};
let configs = [{name: 'foo', type: 'quantity'}];
let configs = [{ name: 'foo', type: 'quantity' }];
let { errors, args } = sanitizer(request, configs);
let quantity = args.foo[0].value[0];
expect(errors).toHaveLength(0);
Expand All @@ -375,10 +384,10 @@ describe('Parameter Sanitization Test', () => {
let request = {
method: 'GET',
query: {
foo: '12|http://unitsofmeasure.org|'
}
foo: '12|http://unitsofmeasure.org|',
},
};
let configs = [{name: 'foo', type: 'quantity'}];
let configs = [{ name: 'foo', type: 'quantity' }];
let { errors, args } = sanitizer(request, configs);
let quantity = args.foo[0].value[0];
expect(errors).toHaveLength(0);
Expand All @@ -392,10 +401,13 @@ describe('Parameter Sanitization Test', () => {
method: 'GET',
query: {
foo: '12||mg',
bar: '12|mg'
}
bar: '12|mg',
},
};
let configs = [{name: 'foo', type: 'quantity'}, {name: 'bar', type: 'quantity'}];
let configs = [
{ name: 'foo', type: 'quantity' },
{ name: 'bar', type: 'quantity' },
];
let { errors, args } = sanitizer(request, configs);
let fooQuantity = args.foo[0].value[0];
let barQuantity = args.bar[0].value[0];
Expand All @@ -412,10 +424,10 @@ describe('Parameter Sanitization Test', () => {
let request = {
method: 'GET',
query: {
foo: 'lt1.010|http://unitsofmeasure.org|kg'
}
foo: 'lt1.010|http://unitsofmeasure.org|kg',
},
};
let configs = [{name: 'foo', type: 'quantity'}];
let configs = [{ name: 'foo', type: 'quantity' }];
let { errors, args } = sanitizer(request, configs);
let quantity = args.foo[0].value[0];
expect(errors).toHaveLength(0);
Expand All @@ -428,27 +440,31 @@ describe('Parameter Sanitization Test', () => {
let request = {
method: 'GET',
query: {
foo: 'lt1.0asdf0|http://unitsofmeasure.org|kg'
}
foo: 'lt1.0asdf0|http://unitsofmeasure.org|kg',
},
};
let configs = [{name: 'foo', type: 'quantity'}];
let configs = [{ name: 'foo', type: 'quantity' }];
let { errors, args } = sanitizer(request, configs);
expect(errors).toHaveLength(1);
expect(errors[0].message).toContain('Expected value: 1 does not equal given value: NaN');
expect(errors[0].message).toContain(
'Expected value: 1 does not equal given value: NaN',
);
expect(Object.getOwnPropertyNames(args)).toHaveLength(0);
});

test('Should throw an error on no number', () => {
let request = {
method: 'GET',
query: {
foo: '|http://unitsofmeasure.org|kg'
}
foo: '|http://unitsofmeasure.org|kg',
},
};
let configs = [{name: 'foo', type: 'quantity'}];
let configs = [{ name: 'foo', type: 'quantity' }];
let { errors, args } = sanitizer(request, configs);
expect(errors).toHaveLength(1);
expect(errors[0].message).toContain('expected quantity.number for parameter foo');
expect(errors[0].message).toContain(
'expected quantity.number for parameter foo',
);
expect(Object.getOwnPropertyNames(args)).toHaveLength(0);
});
});
Expand Down

0 comments on commit a7eb6f1

Please sign in to comment.