Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sc 63647/submission of json objects to string fields #170

Merged
4 changes: 2 additions & 2 deletions lib/ensureString.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');
const { result } = require('lodash');

// input data parsed by these types are expected to be strings;
// this is used to ensure (i.e., to handle objects, etc.)
module.exports = function (data) {
return _.result(data, 'raw.toString', data.toString());
return result(data, 'raw.toString', data.toString());
};
20 changes: 18 additions & 2 deletions lib/types/city.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
const parse = function (string) {
let parsed;
if (typeof string === 'string') {
parsed = string;
} else {
parsed = new String(string);
parsed.raw = string;
parsed.valid = false;
}
return parsed;
};

const components = [
{ name: 'raw', type: 'string', description: 'Unmodified value' }
];

module.exports = {
parse: (str) => str,
components: [],
parse,
components,
maskable: false,
operators: [
'is equal to',
Expand Down
22 changes: 20 additions & 2 deletions lib/types/first_name.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
const parse = function (string) {
let parsed;
if (typeof string === 'string') {
parsed = string;
} else {
parsed = new String(string);
parsed.raw = string;
parsed.valid = false;
}
return parsed;
};

const components = [
{ name: 'raw', type: 'string', description: 'Unmodified value' }
];

module.exports = {
parse: (str) => str,
components: [],
parse,
components,
maskable: false,
operators: [
'is equal to',
'is not equal to',
'is blank',
'is not blank',
'format is valid',
'format is invalid',
'matches pattern',
'does not match pattern'
],
Expand Down
20 changes: 18 additions & 2 deletions lib/types/last_name.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
const parse = function (string) {
let parsed;
if (typeof string === 'string') {
parsed = string;
} else {
parsed = new String(string);
parsed.raw = string;
parsed.valid = false;
}
return parsed;
};

const components = [
{ name: 'raw', type: 'string', description: 'Unmodified value' }
];

module.exports = {
parse: (str) => str,
components: [],
parse,
components,
maskable: false,
operators: [
'is equal to',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"google-libphonenumber": "^3.2.3",
"handlebars": "^4.7.7",
"lodash": "^4.17.21",
"moment": "^2.29.4",
"moment": "2.29.4",
"named-regexp": "^0.1.1"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions test/city.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ describe('City', function () {
it('should have examples', function () {
assert(city.examples.length);
});

it('should handle non-string garbage', function () {
let parsed, stringified;
const invokeStringify = () => {
parsed = city.parse({ foo: 42 });
stringified = JSON.stringify(parsed);
};
assert.doesNotThrow(invokeStringify);

assert.equal(stringified, '"[object Object]"');
assert.equal(parsed.raw, '[object Object]');
assert.isFalse(parsed.valid);
});
});
13 changes: 13 additions & 0 deletions test/first-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ describe('First Name', function () {
it('should have examples', function () {
assert(firstName.examples.length);
});

it('should handle non-string garbage', function () {
let parsed, stringified;
const invokeStringify = () => {
parsed = firstName.parse({ foo: 42 });
stringified = JSON.stringify(parsed);
};
assert.doesNotThrow(invokeStringify);

assert.equal(stringified, '"[object Object]"');
assert.equal(parsed.raw, '[object Object]');
assert.isFalse(parsed.valid);
});
});
13 changes: 13 additions & 0 deletions test/last-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ describe('Last Name', function () {
it('should have examples', function () {
assert(lastName.examples.length);
});

it('should handle non-string garbage', function () {
let parsed, stringified;
const invokeStringify = () => {
parsed = lastName.parse({ foo: 42 });
stringified = JSON.stringify(parsed);
};
assert.doesNotThrow(invokeStringify);

assert.equal(stringified, '"[object Object]"');
assert.equal(parsed.raw, '[object Object]');
assert.isFalse(parsed.valid);
});
});
Loading