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

feat: humanize format #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ValidationError.js
@@ -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
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO =)

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
@@ -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
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
@@ -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
Expand Up @@ -2784,6 +2784,14 @@ describe('Validation', () => {
(msg) => expect(msg).toMatchSnapshot()
);

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

createFailedTestCase(
'$data',
{
Expand Down