Skip to content

Commit

Permalink
Merge pull request #41 from Fcmam5/feature/validate-data
Browse files Browse the repository at this point in the history
🔎 Add data verification
  • Loading branch information
Fcmam5 committed Sep 11, 2020
2 parents 9dc78d7 + b379b62 commit 80044d0
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ jobs:
- run:
name: mutation tests
command: npm run stryker
- run:
name: validate data schemas
command: npm run validate-data-schemas
13 changes: 10 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
"eslint.enable": true,
"workbench.colorCustomizations": {
"titleBar.activeForeground": "#FFFFFF",
"titleBar.activeBackground": "#4CAF50",
"statusBar.background": "#F44336"
"titleBar.activeBackground": "#27ae60",
"statusBar.background": "#c0392b"
},
"window.title": "${dirty}${activeEditorShort}${separator} 🇩🇿 ${rootName}${separator}${appName}",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
},
"cSpell.words": [
"Wilaya",
"Wilayas",
"baladyiats",
"dairats",
"mattricule"
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Find the full API documentation [here](https://documenter.getpostman.com/view/63
* Build a dashboard to manage/correct data
* Match each postal code withe its provinces
* Add long/latt cordinates
* sqlite ?
* Improve data validation (see [`tools/validateDataSchemas.js`](./tools/validateDataSchemas.js) and [`data/WilayaList.schema.json`](./data/WilayaList.schema.json))

## Contributing

Expand Down
119 changes: 119 additions & 0 deletions data/WilayaList.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "Wilaya list",
"type": "array",
"maxLength": 48,
"minLength": 48,
"items": {
"type": "object",
"properties": {
"mattricule": {
"description": "The province (Wilaya) code",
"type": "number",
"minimum": 1,
"maximum": 48
},
"name": {
"type": "string",
"minLength": 3
},
"name_en": {
"type": "string",
"minLength": 3
},
"name_ar": {
"type": "string",
"pattern": "^[\u0621-\u064A\\s]+$"
},
"phoneCodes": {
"type": "array",
"items": {
"type": "number"
}
},
"adjacentWilayas": {
"type": "array",
"items": {
"type": "number"
}
},
"postalCodes": {
"type": "array",
"items": {
"type": "number"
}
},
"dairats": {
"type": "array",
"items": {
"type": "object",
"properties": {
"code": {
"type": "number"
},
"name": {
"type": "string",
"minLength": 3
},
"name_ar": {
"type": "string",
"pattern": "^[\u0621-\u064A\\s\\(\\)]+$"
},
"name_en": {
"type": "string",
"minLength": 3
},
"baladyiats": {
"type": "array",
"items": {
"type": "object",
"properties": {
"code": {
"type": "number"
},
"name": {
"type": "string",
"minLength": 3
},
"name_ar": {
"type": "string",
"pattern": "^[\u0621-\u064A\\s\\(\\)]+$"
},
"name_en": {
"type": "string",
"minLength": 3
}
},
"additionalProperties": false,
"required": [
"code",
"name",
"name_en",
"name_ar"
]
}
}
},
"additionalProperties": false,
"required": [
"code",
"name",
"name_en",
"name_ar"
]
}
}
}
},
"additionalProperties": false,
"required": [
"mattricule",
"name",
"name_en",
"name_ar",
"phoneCodes",
"adjacentWilayas",
"dairats",
"postalCodes"
]
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "algeria-api",
"version": "1.0.0",
"description": "The API for DZ 🇩🇿",
"engines": {
"node": "12.x"
},
"repository": {
"url": "https://github.com/Fcmam5/algeria-api"
},
Expand All @@ -18,9 +21,10 @@
"snyk-protect": "snyk protect",
"prepublishOnly": "npm run snyk-protect",
"lint": "eslint server",
"flush-db": "node server/flush-db/index.js",
"prepare": "yarn run snyk-protect",
"stryker": "stryker run"
"stryker": "stryker run",
"flush-db": "node server/flush-db/index.js",
"validate-data-schemas": "node tools/validateDataSchemas"
},
"keywords": [],
"author": "",
Expand All @@ -30,6 +34,7 @@
"@stryker-mutator/javascript-mutator": "^3.3.1",
"@stryker-mutator/jest-runner": "^3.3.1",
"@types/jest": "^26.0.13",
"ajv": "^6.12.4",
"eslint": "^7.8.1",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-airbnb-base": "^14.2.0",
Expand Down
8 changes: 7 additions & 1 deletion tools/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Crawlers
# Tools

## Validate data

Validate data based on a JSON schema

## Crawlers

## Get postal codes from Poste.dz website

Expand Down
12 changes: 12 additions & 0 deletions tools/validateDataSchemas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const ajv = require('ajv')()
const jsonData = require('../data/WilayaList.json');
const jsonSchema = require('../data/WilayaList.schema.json');

const validateJsonData = ajv.compile(jsonSchema)

if (!validateJsonData(jsonData)) {
console.error(validateJsonData.errors);
process.exit(1)
} else {
console.log('JSON Data is Valid!');
}

0 comments on commit 80044d0

Please sign in to comment.