Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created validation script to prevent extra properties #15

Merged
merged 1 commit into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

workflow_dispatch:

Expand All @@ -21,4 +21,6 @@ jobs:
run: yarn install

- name: Build
run: yarn build
run: |
yarn build
yarn validate
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
index.js
**.js
**.d.ts
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ cache:
- node_modules
script:
- yarn build
- yarn validate
6 changes: 4 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export type Company = {
};
};

export const data: {
export type JobsData = {
[name: string]: Company;
} = rawData;
};

export const data: JobsData = rawData;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"name": "tech-jobs",
"version": "1.0.0",
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"repository": "git@github.com:techcareerio/tech-jobs.git",
"author": "Cat Chen <catchen@catchen.me>",
"license": "MIT",
"devDependencies": {
"ajv": "^7.0.3",
"typescript": "^4.1.3"
},
"scripts": {
"build": "npx tsc"
"build": "npx tsc",
"validate": "node --experimental-json-modules validate.js"
}
}
60 changes: 60 additions & 0 deletions validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import Ajv, { JSONSchemaType, DefinedError } from "ajv";
import type { Opening, Job, Company, JobsData } from "./index";
import data from "./data.json";

const openingSchema: JSONSchemaType<Opening> = {
type: "object",
required: ["available"],
properties: {
available: { type: "boolean" },
url: { type: "string", nullable: true },
},
additionalProperties: false,
};

const jobSchema: JSONSchemaType<Job> = {
type: "object",
required: [],
properties: {
intern: { ...openingSchema, nullable: true },
newGrad: { ...openingSchema, nullable: true },
senior: { ...openingSchema, nullable: true },
manager: { ...openingSchema, nullable: true },
},
additionalProperties: false,
};

const companySchema: JSONSchemaType<Company> = {
type: "object",
required: ["jobs"],
properties: {
url: { type: "string", nullable: true },
jobs: {
type: "object",
required: [],
properties: {
softwareEngineer: { ...jobSchema, nullable: true },
dataScientist: { ...jobSchema, nullable: true },
productManager: { ...jobSchema, nullable: true },
},
additionalProperties: false,
},
},
additionalProperties: false,
};

const schema: JSONSchemaType<JobsData> = {
type: "object",
required: [],
additionalProperties: companySchema,
};

// @ts-ignore
const ajv: Ajv = new Ajv.default();
const validate = ajv.compile(schema);
if (!validate(data)) {
for (const error of validate.errors as DefinedError[]) {
console.error(error);
}
throw new Error("Validation failure");
}
37 changes: 37 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,44 @@
# yarn lockfile v1


ajv@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-7.0.3.tgz#13ae747eff125cafb230ac504b2406cf371eece2"
integrity sha512-R50QRlXSxqXcQP5SvKUrw8VZeypvo12i2IX0EeR5PiZ7bEKeHWgzgo264LDadUsCU42lTJVhFikTqJwNeH34gQ==
dependencies:
fast-deep-equal "^3.1.1"
json-schema-traverse "^1.0.0"
require-from-string "^2.0.2"
uri-js "^4.2.2"

fast-deep-equal@^3.1.1:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

json-schema-traverse@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz#ae7bcb3656ab77a73ba5c49bf654f38e6b6860e2"
integrity sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==

punycode@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

require-from-string@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"
integrity sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==

typescript@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==

uri-js@^4.2.2:
version "4.4.1"
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"
integrity sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==
dependencies:
punycode "^2.1.0"