Skip to content

Commit

Permalink
Merge pull request #1 from hckrnews/feature/validate-sub-arrays
Browse files Browse the repository at this point in the history
Validate sub array's
  • Loading branch information
w3nl authored Feb 19, 2020
2 parents 7ccc057 + b2f6ac4 commit 99a0913
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,36 @@ const personSchema = {
postalCode: "string",
city: "string",
country: "string"
},
companies: {
name: "string",
website: "?string"
}
};
```

Example valid data for the person schema:
```javascript
const personObj = {
name: "James",
age: 25,
siblings: ["Johnnathan"],
metaData: {},
active: true,
address: {
street: "Streetname",
number: 1,
postalCode: "1234AB",
city: "City",
country: "Somewehere"
},
companies: [
{ name: "Example company 1", website: "https://hckr.news" }
{ name: "Example company 2" }
]
}
```

Available types:
* string
* array
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hckrnews/validator",
"version": "1.0.1",
"version": "1.1.0",
"description": "Object validator",
"main": "src/validator.js",
"files": [
Expand All @@ -14,17 +14,17 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/w3nl/validator.git"
"url": "git+https://github.com/hckrnews/validator.git"
},
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
},
"license": "AGPL-3.0",
"bugs": {
"url": "https://github.com/w3nl/validator/issues"
"url": "https://github.com/hckrnews/validator/issues"
},
"homepage": "https://github.com/w3nl/validator#readme",
"homepage": "https://github.com/hckrnews/validator#readme",
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/preset-env": "^7.8.4",
Expand Down
4 changes: 4 additions & 0 deletions src/schemas/company.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
name: "string",
website: "?string"
};
4 changes: 3 additions & 1 deletion src/schemas/person.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import addressSchema from "./address.js";
import companySchema from "./company.js";

export default {
name: "string",
age: "number",
siblings: "array",
metaData: "?object",
active: "boolean",
address: addressSchema
address: addressSchema,
companies: companySchema
};
12 changes: 10 additions & 2 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ export class Validator {
}

if (!types.hasOwnProperty(fieldType)) {
const validator = new Validator(fieldType);
if (value.constructor == Array) {
return value.every(item => {
const validator = new Validator(fieldType);

return validator.validate(value);
return validator.validate(item);
});
} else {
const validator = new Validator(fieldType);

return validator.validate(value);
}
}

const type = types[fieldType];
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ const testCases = [
postalCode: "1234AB",
city: "City",
country: "Somewehere"
}
},
companies: [{ name: "Example", website: "https://hckr.news" }]
},
schema: personSchema,
expectedValue: true
Expand All @@ -84,7 +85,8 @@ const testCases = [
postalCode: "1234AB",
city: "City",
country: "Somewehere"
}
},
companies: [{ name: "Example 1" }, { name: "Example 2" }]
},
schema: personSchema,
expectedValue: true
Expand Down

0 comments on commit 99a0913

Please sign in to comment.