Skip to content

Commit f7e9e93

Browse files
author
Daniel Morse
committed
feat: convert schema data to camelCase before calling validate
1 parent 0800cc1 commit f7e9e93

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@webcomponents/template": "^1.4.0",
1515
"@webcomponents/webcomponentsjs": "^2.2.4",
1616
"ajv": "^6.7.0",
17+
"change-case": "^3.1.0",
1718
"classnames": "^2.2.6",
1819
"color": "^3.1.0",
1920
"core-js": "^2.6.3",

packages/core/renderers/bolt-base.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Ajv from 'ajv';
22
import { withComponent, shadow, props } from 'skatejs';
3-
import { findParentTag, hasNativeShadowDomSupport } from '../utils';
3+
import changeCase from 'change-case';
4+
import { findParentTag, hasNativeShadowDomSupport, renameKey } from '../utils';
45

56
export function BoltBase(Base = HTMLElement) {
67
return class extends Base {
@@ -74,8 +75,21 @@ export function BoltBase(Base = HTMLElement) {
7475
}
7576
}
7677

77-
if (this.schema) {
78-
let isValid = ajv.validate(this.schema, validatedData);
78+
// Skip this if formatted schema data is already stored
79+
if (this.schema && !this.formattedSchema) {
80+
this.formattedSchema = {};
81+
Object.assign(this.formattedSchema, this.schema);
82+
Object.keys(this.formattedSchema.properties).map(key => {
83+
this.formattedSchema.properties = renameKey(
84+
key,
85+
changeCase.camelCase(key),
86+
this.formattedSchema.properties,
87+
);
88+
});
89+
}
90+
91+
if (this.formattedSchema) {
92+
let isValid = ajv.validate(this.formattedSchema, validatedData);
7993

8094
// bark at any schema validation errors
8195
if (!isValid) {

packages/core/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from './find-parent-tag';
88
export * from './get-component-root-element';
99
export * from './is-valid-selector';
1010
export * from './rgb2hex';
11+
export * from './rename-key';
1112
export * from './sanitize-classes';
1213
export * from './supports-css-vars';
1314
export * from './supports-passive-event-listener';

packages/core/utils/rename-key.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Immutably renames object keys
3+
* https://medium.com/front-end-weekly/immutably-rename-object-keys-in-javascript-5f6353c7b6dd
4+
*
5+
* @param {String} oldKey - The name of the key to be renamed
6+
* @param {String} newKey - The name it will be renamed it to
7+
* @param {Object} object - The object that contains the old key name
8+
* @returns {Object} - A new object with one renamed key
9+
*/
10+
11+
export const renameKey = (oldKey, newKey, { [oldKey]: value, ...object }) => ({
12+
[newKey]: value,
13+
...object,
14+
});

0 commit comments

Comments
 (0)