From 94c001b3a0bd7eaa510520cfbc71149305523623 Mon Sep 17 00:00:00 2001 From: Abdallah Al-Barmawi Date: Wed, 3 Jan 2024 23:53:11 +0100 Subject: [PATCH] Setup company vaidation --- .gitignore | 1 + .husky/pre-commit | 4 + README.md | 8 +- package-lock.json | 90 +++++++++++++++++++++ package.json | 20 +++++ validators/companies/companies-validator.js | 41 ++++++++++ validators/companies/company-schema.yaml | 35 ++++++++ 7 files changed, 196 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100755 .husky/pre-commit create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 validators/companies/companies-validator.js create mode 100644 validators/companies/company-schema.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..f0bc84e --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +npm run validate diff --git a/README.md b/README.md index 1539e67..8478595 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ We welcome contributions from the community. If you wish to add a company to the - Add a comprehensive commit message e.g. "Added new trigger-happy company". Push. - Submit a pull request with a detailed description of your additions or changes. - #### Help! I'm not a developer! How do I contribute? 1. Create a [github account](https://github.com/). @@ -38,7 +37,10 @@ We welcome contributions from the community. If you wish to add a company to the Done! One of the project maintainers will review and merge your changes. +### Data Schema + +- [Company Schema](./validators/companies/company-schema.yaml) -### Next steps +### Available commands -- Validate companies data schema +- `npm run validate` Validate the companies yaml files diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f585ed8 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,90 @@ +{ + "name": "boycott-israeli-tech-companies-dataset", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "boycott-israeli-tech-companies-dataset", + "version": "1.0.0", + "license": "ISC", + "devDependencies": { + "ajv": "^8.12.0", + "husky": "^8.0.0", + "yaml": "^2.3.4" + } + }, + "node_modules/ajv": { + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.12.0.tgz", + "integrity": "sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==", + "dependencies": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" + }, + "node_modules/husky": { + "version": "8.0.3", + "resolved": "https://registry.npmjs.org/husky/-/husky-8.0.3.tgz", + "integrity": "sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg==", + "dev": true, + "bin": { + "husky": "lib/bin.js" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, + "node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==" + }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "engines": { + "node": ">=6" + } + }, + "node_modules/require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/uri-js": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", + "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", + "dependencies": { + "punycode": "^2.1.0" + } + }, + "node_modules/yaml": { + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.3.4.tgz", + "integrity": "sha512-8aAvwVUSHpfEqTQ4w/KMlf3HcRdt50E5ODIQJBw1fQ5RL34xabzxtUlzTXVqc4rkZsPbvrXKWnABCD7kWSmocA==", + "engines": { + "node": ">= 14" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..4dfe59f --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "boycott-israeli-tech-companies-dataset", + "version": "1.0.0", + "description": "This repository is dedicated to providing a comprehensive list of Israeli tech companies to boycott, categorized based on their respective industries.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "validate": "node validators/companies/companies-validator.js", + "prepare": "husky install" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "ajv": "^8.12.0", + "husky": "^8.0.0", + "yaml": "^2.3.4" + }, + "dependencies": {} +} diff --git a/validators/companies/companies-validator.js b/validators/companies/companies-validator.js new file mode 100644 index 0000000..fad6ad0 --- /dev/null +++ b/validators/companies/companies-validator.js @@ -0,0 +1,41 @@ +// Validate companies using JSON Schema + +const fs = require("fs"); +const path = require("path"); +const yaml = require("yaml"); +const Ajv = require("ajv"); + +const companiesDatasetDir = path.join(__dirname, "../../", "dataset/companies"); +const companySchema = path.join( + __dirname, + "../../", + "validators/companies/company-schema.yaml" +); + +// Load and parse YAML schema file +const schemaFile = fs.readFileSync(companySchema, "utf8"); +const schema = yaml.parse(schemaFile); + +// Create an Ajv instance +const ajv = new Ajv(); +const validate = ajv.compile(schema); + +// Get all categories (company files) +const categories = fs.readdirSync(companiesDatasetDir); + +// Validate files 1 by 1 +for (category of categories) { + const companies = fs.readFileSync( + `${companiesDatasetDir}/${category}`, + "utf8" + ); + const data = yaml.parse(companies); + + const valid = validate(data); + if (!valid) { + console.log("Validation errors:", validate.errors); + process.exit(1); + } +} + +console.log("YAML data is valid!"); diff --git a/validators/companies/company-schema.yaml b/validators/companies/company-schema.yaml new file mode 100644 index 0000000..f5649ab --- /dev/null +++ b/validators/companies/company-schema.yaml @@ -0,0 +1,35 @@ +type: array +items: + type: object + properties: + Name: + type: string + maxLength: 50 + Description: + type: string + maxLength: 500 + Website: + type: string + maxLength: 50 + Alternatives: + type: array + items: + type: object + properties: + Name: + type: string + maxLength: 50 + Description: + type: string + maxLength: 500 + Website: + type: string + maxLength: 50 + required: + - Name + - Website + additionalProperties: false + required: + - Name + - Website + additionalProperties: false