Skip to content

Commit

Permalink
feat: humanize format
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Oct 13, 2019
1 parent 7fc8069 commit d3eb673
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/ValidationError.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const humanize = require('./util/humanize');
const Range = require('./util/Range');

const SPECIFICITY = {
Expand Down Expand Up @@ -427,7 +428,7 @@ class ValidationError extends Error {
}

if (schema.format) {
hints.push(`should match format ${JSON.stringify(schema.format)}`);
type = `${humanize(schema.format)} ${type}`;
}

if (schema.formatMinimum) {
Expand Down
6 changes: 5 additions & 1 deletion src/keywords/absolutePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function getErrorFor(shouldBeAbsolute, data, schema) {
return errorMessage(schema, data, message);
}

export default (ajv) =>
export default (ajv) => {
ajv.addFormat('absolutePath', () => true);
ajv.addKeyword('absolutePath', {
errors: true,
type: 'string',
Expand Down Expand Up @@ -54,3 +55,6 @@ export default (ajv) =>
return callback;
},
});

return ajv;
};
57 changes: 57 additions & 0 deletions src/util/humanize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Transform provided string
* from camelCase, PascalCase, dash-case, snake_case
* to human readable string
* @param {string} str provided string
* @returns {string}
*/
module.exports = function humanize(str) {
if (typeof str !== 'string' || str.length < 2) {
return str;
}
const tryDashCase = str.split('-');

if (tryDashCase.length !== 1) {
return tryDashCase.join(' ').toLowerCase();
}

const stringWithValidStart = str[0] === '_' ? str.slice(1) : str;
const trySnakeCase = stringWithValidStart.split('_');

if (trySnakeCase.length !== 1) {
return trySnakeCase.join(' ').toLowerCase();
}

const aUpperCase = 'A'.charCodeAt(0);
const zUpperCase = 'Z'.charCodeAt(0);
/** @type {string[]} */
const words = [];

let lastUpperCase = 0;
let i = 1;

/**
* @param {string} word
* @param {number} j
* @returns {boolean}
*/
function isUpperCase(word, j) {
const code = word.charCodeAt(j);

return code >= aUpperCase && code <= zUpperCase;
}

while (i < stringWithValidStart.length) {
while (
i < stringWithValidStart.length &&
!isUpperCase(stringWithValidStart, i)
)
i += 1;

words.push(stringWithValidStart.slice(lastUpperCase, i).toLowerCase());
lastUpperCase = i;
i += 1;
}

return words.join(' ');
};
25 changes: 17 additions & 8 deletions test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions test/fixtures/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3708,6 +3708,18 @@
"type": "string",
"format": { "$data": "0#" }
}
},
"absolutePath": {
"anyOf": [
{
"type": "string",
"minLength": 1,
"format": "absolutePath"
},
{
"type": "number"
}
]
}
}
}
19 changes: 19 additions & 0 deletions test/humanize.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import humanize from '../src/util/humanize';

const words = [
['dash-case', 'dash case'],
['_snake_case', 'snake case'],
['snake-case', 'snake case'],
['PascalCase', 'pascal case'],
['_12Integers', '12 integers'],
['awesomeStringFormat13', 'awesome string format13'],
['camelCase', 'camel case'],
];

describe('humanize', () => {
words.forEach(([provided, expected]) => {
it(provided, () => {
expect(humanize(provided)).toEqual(expected);
});
});
});
8 changes: 8 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2784,6 +2784,14 @@ describe('Validation', () => {
(msg) => expect(msg).toMatchSnapshot()
);

createFailedTestCase(
'format #1',
{
absolutePath: {},
},
(msg) => expect(msg).toMatchSnapshot()
);

createFailedTestCase(
'$data',
{
Expand Down

0 comments on commit d3eb673

Please sign in to comment.