Skip to content

Commit

Permalink
Merge pull request #85 from hckrnews/fix-optional-keys
Browse files Browse the repository at this point in the history
Fix the optional keys
  • Loading branch information
w3nl authored Apr 16, 2021
2 parents acb9a77 + 71c684c commit 007e4ca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hckrnews/objects",
"description": "Hack JavaScript objects",
"version": "2.2.2",
"version": "2.2.3",
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
Expand Down
20 changes: 19 additions & 1 deletion src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ const ObjectGenerator = ({ schema } = {}) =>
}

get subSchema() {
return this.subPrefix ? schema[this.subPrefix] : schema;
const subSchema = this.subPrefix ? schema[this.subPrefix] : schema;

if (subSchema) {
return subSchema;
}

if (schema[`${this.subPrefix}?`]) {
return schema[`${this.subPrefix}?`];
}

if (schema[`?${this.subPrefix}`]) {
return schema[`?${this.subPrefix}`];
}

return null;
}

get validator() {
Expand All @@ -48,6 +62,10 @@ const ObjectGenerator = ({ schema } = {}) =>
validate() {
const { validator } = this;

if (!validator) {
return;
}

this.originalData.forEach((data) => {
if (!validator.validate(data)) {
const [field, type] = validator.errors[0];
Expand Down
4 changes: 2 additions & 2 deletions src/schemas/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import Test3Schema from './test3';

export default {
name: String,
test: Test2,
test3: Test3Schema
'?test': Test2,
'test3?': Test3Schema
};

0 comments on commit 007e4ca

Please sign in to comment.