From d3154609eff026454a9fbb191f359463965c5121 Mon Sep 17 00:00:00 2001 From: Toni Ruottu Date: Wed, 16 Aug 2023 11:04:34 +0300 Subject: [PATCH] Create minimal reference schemas --- .../cd-maasglobal-reference-schemas.yml | 35 +++++++++ .github/workflows/ci.yml | 18 +++++ maasglobal-reference-schemas/.gitignore | 12 +++ maasglobal-reference-schemas/.npmignore | 13 ++++ maasglobal-reference-schemas/.prettierrc.js | 1 + maasglobal-reference-schemas/README.md | 9 +++ maasglobal-reference-schemas/package.json | 32 ++++++++ .../schemas/array.json | 16 ++++ .../schemas/combinator.json | 31 ++++++++ .../schemas/comment.json | 10 +++ .../schemas/enum.json | 33 ++++++++ .../schemas/hyper.json | 78 +++++++++++++++++++ .../schemas/literal.json | 19 +++++ .../schemas/nominal.json | 27 +++++++ .../schemas/number.json | 33 ++++++++ .../schemas/record.json | 23 ++++++ maasglobal-reference-schemas/schemas/set.json | 16 ++++ .../schemas/string.json | 24 ++++++ .../schemas/struct.json | 47 +++++++++++ .../schemas/tuple.json | 17 ++++ maasglobal-reference-schemas/yarn.lock | 13 ++++ 21 files changed, 507 insertions(+) create mode 100644 .github/workflows/cd-maasglobal-reference-schemas.yml create mode 100644 maasglobal-reference-schemas/.gitignore create mode 100644 maasglobal-reference-schemas/.npmignore create mode 100644 maasglobal-reference-schemas/.prettierrc.js create mode 100644 maasglobal-reference-schemas/README.md create mode 100644 maasglobal-reference-schemas/package.json create mode 100644 maasglobal-reference-schemas/schemas/array.json create mode 100644 maasglobal-reference-schemas/schemas/combinator.json create mode 100644 maasglobal-reference-schemas/schemas/comment.json create mode 100644 maasglobal-reference-schemas/schemas/enum.json create mode 100644 maasglobal-reference-schemas/schemas/hyper.json create mode 100644 maasglobal-reference-schemas/schemas/literal.json create mode 100644 maasglobal-reference-schemas/schemas/nominal.json create mode 100644 maasglobal-reference-schemas/schemas/number.json create mode 100644 maasglobal-reference-schemas/schemas/record.json create mode 100644 maasglobal-reference-schemas/schemas/set.json create mode 100644 maasglobal-reference-schemas/schemas/string.json create mode 100644 maasglobal-reference-schemas/schemas/struct.json create mode 100644 maasglobal-reference-schemas/schemas/tuple.json create mode 100644 maasglobal-reference-schemas/yarn.lock diff --git a/.github/workflows/cd-maasglobal-reference-schemas.yml b/.github/workflows/cd-maasglobal-reference-schemas.yml new file mode 100644 index 000000000..ca56f280f --- /dev/null +++ b/.github/workflows/cd-maasglobal-reference-schemas.yml @@ -0,0 +1,35 @@ +name: CD maasglobal-reference-schemas + +on: + push: + branches: + - main + paths: + - 'maasglobal-reference-schemas/package.json' + - '.github/workflows/cd-maasglobal-reference-schemas.yml' + +defaults: + run: + working-directory: maasglobal-reference-schemas + +env: + NODE_AUTH_TOKEN: ${{ secrets.PUBLIC_NPM_TOKEN }} + +jobs: + publish: + runs-on: ubuntu-latest + timeout-minutes: 15 + name: publish + steps: + - uses: actions/checkout@v3 + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: "14" + registry-url: "https://registry.npmjs.org" + - name: Install Dependencies + run: | + yarn + - name: Deploy to Registry + run: | + yarn deploy-npm diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5cc83e1ad..289957422 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,6 +83,24 @@ jobs: run: | yarn --cwd maasglobal-schema-generator-io-ts ci + test-reference-schemas: + runs-on: ubuntu-latest + timeout-minutes: 15 + name: test reference schemas + steps: + - uses: actions/checkout@v3 + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: "14" + registry-url: "https://registry.npmjs.org" + - name: Install Dependencies + run: | + yarn --cwd maasglobal-reference-schemas + - name: Run Tests + run: | + yarn --cwd maasglobal-reference-schemas ci + test-maas-schemas: runs-on: ubuntu-latest timeout-minutes: 15 diff --git a/maasglobal-reference-schemas/.gitignore b/maasglobal-reference-schemas/.gitignore new file mode 100644 index 000000000..27c5c81ff --- /dev/null +++ b/maasglobal-reference-schemas/.gitignore @@ -0,0 +1,12 @@ +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules + +# IDE Stuff +**/.idea +.vscode/launch.json +*.swp + +# OS STUFF +.DS_Store +.tmp diff --git a/maasglobal-reference-schemas/.npmignore b/maasglobal-reference-schemas/.npmignore new file mode 100644 index 000000000..b5dea1f98 --- /dev/null +++ b/maasglobal-reference-schemas/.npmignore @@ -0,0 +1,13 @@ +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules +package-lock.json + +# IDE Stuff +**/.idea +.vscode/launch.json +*.swp + +# OS STUFF +.DS_Store +.tmp diff --git a/maasglobal-reference-schemas/.prettierrc.js b/maasglobal-reference-schemas/.prettierrc.js new file mode 100644 index 000000000..d52ff9a4e --- /dev/null +++ b/maasglobal-reference-schemas/.prettierrc.js @@ -0,0 +1 @@ +module.exports = require('eslint-config-maasglobal-ts/prettierrc.js'); diff --git a/maasglobal-reference-schemas/README.md b/maasglobal-reference-schemas/README.md new file mode 100644 index 000000000..96b49b14b --- /dev/null +++ b/maasglobal-reference-schemas/README.md @@ -0,0 +1,9 @@ +# MaaS Global Reference Schemas + +This repository contains definitions and examples for the JSON Schema format used by MaaS Global. +The format is currently a subset of `http://json-schema.org/draft-07/schema#` and +`https://json-schema.org/draft/2019-09/vocab/hyper-schema`. + +The way we describe tuples seem to be from some earlier version of the specification. + +Also, our schemas contain a custom keyword `invalid` with negative test cases. diff --git a/maasglobal-reference-schemas/package.json b/maasglobal-reference-schemas/package.json new file mode 100644 index 000000000..0a4d0a637 --- /dev/null +++ b/maasglobal-reference-schemas/package.json @@ -0,0 +1,32 @@ +{ + "name": "maasglobal-reference-schemas", + "version": "0.0.1", + "description": "Test suite for JSON schema features used by MaaS Global", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/maasglobal/maas-schemas.git" + }, + "keywords": [ + "schemas", + "JSON" + ], + "bugs": { + "url": "https://github.com/maasglobal/maas-schemas/issues" + }, + "homepage": "https://github.com/maasglobal/maas-schemas/", + "dependencies": {}, + "devDependencies": { + "eslint-config-maasglobal-ts": "^0.0.14", + "prettier": "^2.8.1" + }, + "scripts": { + "prettier-check": "yarn prettier --check '**/*.{css,html,js,ts,json,md,yaml,yml}'", + "prettier-fix": "yarn prettier-check --write", + "prettify": "yarn prettier-fix", + "lint": "yarn prettier-check", + "ci": "yarn lint", + "deploy-npm": "yarn ci && yarn publish --non-interactive", + "deploy-alpha": "yarn deploy-npm --tag alpha" + } +} diff --git a/maasglobal-reference-schemas/schemas/array.json b/maasglobal-reference-schemas/schemas/array.json new file mode 100644 index 000000000..7bd27ac77 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/array.json @@ -0,0 +1,16 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/array.json", + "title": "Array", + "description": "An ordered collection of homogenous values, with duplicates allowed. See Set and Tuple for other array based structures.", + "definitions": { + "exampleArray": { + "type": "array", + "items": { "type": "string" }, + "contains": { "type": "string", "const": "foo" }, + "minItems": 1, + "maxItems": 10, + "examples": [["asdf", "foo", "qwer"]] + } + } +} diff --git a/maasglobal-reference-schemas/schemas/combinator.json b/maasglobal-reference-schemas/schemas/combinator.json new file mode 100644 index 000000000..e5dbebccd --- /dev/null +++ b/maasglobal-reference-schemas/schemas/combinator.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/combinator.json", + "title": "Combinator Reference Schemas", + "description": "Keywords that combine several schemas into one.", + "definitions": { + "nonNegative": { + "type": "number", + "minimum": 0 + }, + "integer": { + "type": "integer" + }, + "nonNegativeInteger": { + "allOf": [ + { "$ref": "#/definitions/nonNegative" }, + { "$ref": "#/definitions/integer" } + ] + }, + + "email": { + "type": "string" + }, + "username": { + "type": "string" + }, + "login": { + "anyOf": [{ "$ref": "#/definitions/email" }, { "$ref": "#/definitions/username" }] + } + } +} diff --git a/maasglobal-reference-schemas/schemas/comment.json b/maasglobal-reference-schemas/schemas/comment.json new file mode 100644 index 000000000..ac526a5c4 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/comment.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/comment.json", + "$comment": "This is a toplevel comment", + "definitions": { + "exampleDefinition": { + "$comment": "This is a definition comment" + } + } +} diff --git a/maasglobal-reference-schemas/schemas/enum.json b/maasglobal-reference-schemas/schemas/enum.json new file mode 100644 index 000000000..2de31e288 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/enum.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/enum.json", + "title": "Enum Reference Schemas", + "description": "Basic enum definitions should work as expected. Separate accessors can be defined for each value.", + "definitions": { + "timeOfDay": { + "type": "string", + "enum": ["night", "day"], + "examples": ["night", "day"] + }, + "timeOfDayNIGHT": { + "$comment": "Example accessor", + "default": "night", + "allOf": [ + { + "$ref": "#/definitions/timeOfDay" + }, + { "const": "night" } + ] + }, + "timeOfDayDAY": { + "$comment": "Another example accessor", + "default": "day", + "allOf": [ + { + "$ref": "#/definitions/timeOfDay" + }, + { "const": "day" } + ] + } + } +} diff --git a/maasglobal-reference-schemas/schemas/hyper.json b/maasglobal-reference-schemas/schemas/hyper.json new file mode 100644 index 000000000..4ed61ad27 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/hyper.json @@ -0,0 +1,78 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/hyper.json", + "title": "Hyper Schema Reference", + "description": "Example endpoint", + "definitions": { + "pizzaId": { "type": "string" }, + "recipe": { + "type": "object", + "properties": { + "pizza": { "$ref": "#/definitions/pizzaId" }, + "ingredients": { "type": "object" }, + "steps": { "type": "array" } + }, + "additionalProperties": false + }, + "unknownPizza": { + "type": "null", + "default": null + }, + "withGarlic": { + "type": "boolean" + }, + "request": { + "type": "object", + "properties": { + "garlic": { "$ref": "#/definitions/withGarlic" } + }, + "required": ["garlic"], + "additionalProperties": false + }, + "response": { + "anyOf": [ + { "$ref": "#/definitions/unknownPizza" }, + { "$ref": "#/definitions/recipe" } + ] + } + }, + "links": [ + { + "rel": "implementation", + + "href": "{+api}pizza/{pizzaId}/recipe", + "hrefSchema": { + "type": "object", + "properties": { + "api": { + "type": "string", + "const": "https://example.com/" + }, + "pizzaId": { + "$ref": "#/definitions/pizzaId" + } + }, + "required": ["api", "pizzaId"], + "additionalProperties": false + }, + + "headerSchema": { + "content-type": { + "type": "string", + "const": "application/json" + } + }, + "submissionSchema": { + "$ref": "#/definitions/request" + }, + + "targetHints": { + "content-type": ["application/json"], + "allow": ["POST"] + }, + "targetSchema": { + "$ref": "#/definitions/response" + } + } + ] +} diff --git a/maasglobal-reference-schemas/schemas/literal.json b/maasglobal-reference-schemas/schemas/literal.json new file mode 100644 index 000000000..3f0b1a2ef --- /dev/null +++ b/maasglobal-reference-schemas/schemas/literal.json @@ -0,0 +1,19 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/literal.json", + "title": "Literal Reference Schemas", + "description": "Literals can define any JSON structure, and the input needs to match that structure", + "definitions": { + "stringLiteral": { + "type": "string", + "const": "foo", + "default": "foo" + }, + + "numberLiteral": { + "type": "number", + "const": 123, + "default": 123 + } + } +} diff --git a/maasglobal-reference-schemas/schemas/nominal.json b/maasglobal-reference-schemas/schemas/nominal.json new file mode 100644 index 000000000..d3d8128b7 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/nominal.json @@ -0,0 +1,27 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/nominal.json", + "title": "Nominal Typing", + "description": "Naming basic types is useful as documentation. In addition the TypeScript we generate has nominal typing to prevent assigning unrelated basic types to each other.", + "definitions": { + "customerId": { + "type": "string" + }, + "age": { + "type": "integer" + }, + "distance": { + "type": "number" + }, + "membership": { + "type": "boolean" + }, + "metadata": { + "type": "object" + }, + "unknownCustomer": { + "type": "null", + "default": null + } + } +} diff --git a/maasglobal-reference-schemas/schemas/number.json b/maasglobal-reference-schemas/schemas/number.json new file mode 100644 index 000000000..8ffce3871 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/number.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/number.json", + "title": "Number Reference Schemas", + "description": "We use several keywords to limit numbers to a specific form", + "definitions": { + "integer": { + "type": "integer" + }, + "positiveNumber": { + "type": "number", + "minimum": 1, + "examples": [1, 2], + "invalid": { + "negative number": "LTE=", + "zero": "MA==" + } + }, + "evenNumber": { + "type": "number", + "multipleOf": 2, + "examples": [2, 4], + "invalid": { + "odd number": "Mw==" + } + }, + "portNumber": { + "type": "number", + "minimum": 0, + "maximum": 65535 + } + } +} diff --git a/maasglobal-reference-schemas/schemas/record.json b/maasglobal-reference-schemas/schemas/record.json new file mode 100644 index 000000000..409066feb --- /dev/null +++ b/maasglobal-reference-schemas/schemas/record.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/record.json", + "title": "Record Reference Schemas", + "description": "Record represents a hashmap from a key type to a value type", + "definitions": { + "exampleId": { "type": "string" }, + "exampleField": { "type": "string" }, + "exampleStruct": { + "type": "object", + "properties": { + "id": { "$ref": "#/definitions/exampleId" }, + "field": { "$ref": "#/definitions/exampleField" } + }, + "additionalProperties": false + }, + "exampleRecord": { + "type": "object", + "propertyNames": { "$ref": "#/definitions/exampleId" }, + "additionalProperties": { "$ref": "#/definitions/exampleStruct" } + } + } +} diff --git a/maasglobal-reference-schemas/schemas/set.json b/maasglobal-reference-schemas/schemas/set.json new file mode 100644 index 000000000..7c737ff08 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/set.json @@ -0,0 +1,16 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/set.json", + "title": "Set Reference schemas", + "description": "Collection of unique items", + "definitions": { + "exmapleSet": { + "type": "array", + "uniqueItems": true, + "examples": [[1, 2, 3]], + "invalid": { + "duplicate items": "WzEsMiwxXQo=" + } + } + } +} diff --git a/maasglobal-reference-schemas/schemas/string.json b/maasglobal-reference-schemas/schemas/string.json new file mode 100644 index 000000000..9bcfec209 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/string.json @@ -0,0 +1,24 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/string.json", + "title": "String Reference Schemas", + "description": "We use several keywords to limit strings to a specific form", + "definitions": { + "exampleString": { + "type": "string", + "minLength": "1", + "maxLength": "10", + "pattern": "^[a-z]+$", + "examples": ["asdf"], + "invalid": { + "too long": "YWJjZGVmZ2hpams=" + } + }, + + "exampleIp": { + "type": "string", + "format": "ipv4", + "examples": ["127.0.0.1"] + } + } +} diff --git a/maasglobal-reference-schemas/schemas/struct.json b/maasglobal-reference-schemas/schemas/struct.json new file mode 100644 index 000000000..ece200d24 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/struct.json @@ -0,0 +1,47 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/struct.json", + "title": "Struct Reference Schemas", + "description": "Structs contain fields with values of specific type dictated by the key", + "definitions": { + "struct": { + "type": "object", + "properties": { + "foo": { + "$ref": "https://reference-schemas.maas.global/foo.json#/definitions/string" + }, + "bar": { + "$ref": "https://reference-schemas.maas.global/foo.json#/definitions/number" + } + }, + "required": ["foo", "bar"], + "additionalProperties": false, + "examples": [ + { + "foo": "asdf", + "bar": 4 + } + ] + }, + "patterns": { + "type": "object", + "patternProperties": { + "^s": { + "$ref": "https://reference-schemas.maas.global/foo.json#/definitions/string" + }, + "^n": { + "$ref": "https://reference-schemas.maas.global/foo.json#/definitions/number" + } + }, + "additionalProperties": false, + "examples": [ + { + "s1": "asdf", + "s2": "qwer", + "n1": 2, + "n2": 4 + } + ] + } + } +} diff --git a/maasglobal-reference-schemas/schemas/tuple.json b/maasglobal-reference-schemas/schemas/tuple.json new file mode 100644 index 000000000..8ea608270 --- /dev/null +++ b/maasglobal-reference-schemas/schemas/tuple.json @@ -0,0 +1,17 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://reference-schemas.maas.global/tuple.json", + "title": "Tuple Reference schemas", + "description": "An ordered collection of items, the type of which depend on their position in the collection", + "definitions": { + "tuple": { + "type": "array", + "items": [ + { "$ref": "https://reference-schemas.maas.global/foo.json#/definitions/string" }, + { "$ref": "https://reference-schemas.maas.global/foo.json#/definitions/number" } + ], + "additionalItems": false, + "examples": [["asdf", 2]] + } + } +} diff --git a/maasglobal-reference-schemas/yarn.lock b/maasglobal-reference-schemas/yarn.lock new file mode 100644 index 000000000..d131dfa0a --- /dev/null +++ b/maasglobal-reference-schemas/yarn.lock @@ -0,0 +1,13 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +eslint-config-maasglobal-ts@^0.0.14: + version "0.0.14" + resolved "https://registry.yarnpkg.com/eslint-config-maasglobal-ts/-/eslint-config-maasglobal-ts-0.0.14.tgz#7b5aa74768faf4cc840478306c453174c1412c11" + integrity sha512-qKkJToBl7efSO8EdQUGCliLuqrjd00RT928X6HcNLYP8wNQE/DJTdfnoZ4GyuGbT9KFi/ye8XQZCUDs5ZMsqXg== + +prettier@^2.8.1: + version "2.8.8" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da" + integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==