Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

add json schemas #4

Merged
merged 56 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
b021254
chore: npm i ajv ajv-cli
AndyOGo Mar 5, 2019
79816f1
feat: setup basic schemas
AndyOGo Mar 5, 2019
4abb538
feat: implement schema for var
AndyOGo Mar 5, 2019
6a49a69
feat: start adding strict equal schema
AndyOGo Mar 5, 2019
8655dfb
feat: start adding strict not-equal schema
AndyOGo Mar 5, 2019
dd903eb
feat: start adding equal schema
AndyOGo Mar 5, 2019
7c4db53
feat: start adding not-equal schema
AndyOGo Mar 5, 2019
3395183
refactore: extract common schemas
AndyOGo Mar 5, 2019
f076b21
refactore: reuse common schemas
AndyOGo Mar 5, 2019
3f7192d
refactore: extracted var into common
AndyOGo Mar 5, 2019
ff5b6dc
refactore: resue common var
AndyOGo Mar 5, 2019
4f3ffca
feat: added missing_some schema
AndyOGo Mar 5, 2019
9b48163
feat: added any and unary common schemas
AndyOGo Mar 6, 2019
4579c0e
feat: added thruthy and falsy schemas
AndyOGo Mar 6, 2019
d620029
feat: added common one-or-more args schema
AndyOGo Mar 7, 2019
a4adbb2
feat: added and schema
AndyOGo Mar 7, 2019
ef11e87
feat: added or shema
AndyOGo Mar 7, 2019
8da53db
feat: added binary arg schema
AndyOGo Mar 7, 2019
322305e
feat: added common trinary arg schema
AndyOGo Mar 7, 2019
c8afad6
feat: added greater schema
AndyOGo Mar 7, 2019
3373fd3
feat: added greater equal schema
AndyOGo Mar 7, 2019
29d2b26
feat: added lower schema
AndyOGo Mar 7, 2019
a54c284
feat: added lower euwal schema
AndyOGo Mar 7, 2019
7208b32
refactore: renamed truthy and falsy sheams to notnot and not
AndyOGo Mar 7, 2019
61b7d34
refactore: renamed lower to less
AndyOGo Mar 7, 2019
ef7fd01
refactore: clarrify all types
AndyOGo Mar 7, 2019
785e28a
refactore: clarify binary arg
AndyOGo Mar 7, 2019
1ec78ae
refactore: clarify one-or-more
AndyOGo Mar 7, 2019
086c9bd
refactore: clarify trinary args
AndyOGo Mar 7, 2019
1dc98b8
refactore: clarify unary arg
AndyOGo Mar 7, 2019
1e20688
fix: wrong paths for ids and refs
AndyOGo Mar 7, 2019
8135b03
feat: added max schema
AndyOGo Mar 7, 2019
ff1d42c
feat: added min schema
AndyOGo Mar 7, 2019
04d49f2
feat: added add schema
AndyOGo Mar 7, 2019
66038be
feat: added divide schema
AndyOGo Mar 7, 2019
ae9c3a0
feat: added modulo schema
AndyOGo Mar 7, 2019
89e5588
feat: added multiply schema
AndyOGo Mar 7, 2019
e4c3dfe
feat: added substract schema
AndyOGo Mar 7, 2019
5fd8381
feat: added log schema
AndyOGo Mar 14, 2019
65d2af6
feat: added boilerplate schema for remaining array and string schemas
AndyOGo Mar 14, 2019
ed8cb06
fix: added missing required field
AndyOGo Mar 14, 2019
8e8d87e
feat: added if schema
AndyOGo Mar 14, 2019
59b156f
feat: precise array args
AndyOGo Mar 14, 2019
368a8a2
feat: added in schema
AndyOGo Mar 14, 2019
19d36a1
feat: added all ops to any
AndyOGo Mar 14, 2019
6ad80d8
fix: invalid json schema
AndyOGo Mar 14, 2019
6feef28
teat: added lint script for json schemas themselve
AndyOGo Mar 14, 2019
d70bf54
fix: comparison ops are binary args
AndyOGo Mar 14, 2019
845e8cb
fix: oneOf will never work with basic any typing...
AndyOGo Mar 14, 2019
a0db8a7
chore: npm i glob
AndyOGo Mar 14, 2019
ce9c892
test: validate all remote test casesby json schema
AndyOGo Mar 14, 2019
0753e40
feat: added unknown method schema
AndyOGo Mar 15, 2019
aa199c3
feat: added title and description for JSON Logic
AndyOGo Mar 15, 2019
e77dbb6
feat: added description for method
AndyOGo Mar 15, 2019
e60b575
fix: case for missing_some
AndyOGo Mar 15, 2019
1827239
fix: typo
AndyOGo Mar 15, 2019
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"@babel/core": "^7.3.3",
"@babel/plugin-proposal-export-default-from": "^7.2.0",
"@babel/preset-env": "^7.3.1",
"ajv": "^6.10.0",
"ajv-cli": "^3.0.0",
"cross-env": "^5.2.0",
"eslint": "^5.14.1",
"eslint-config-airbnb": "^17.1.0",
Expand All @@ -24,6 +26,7 @@
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.12.4",
"glob": "^7.1.3",
"husky": "^1.3.1",
"lint-staged": "^8.1.4",
"prettier": "^1.16.4",
Expand All @@ -40,6 +43,7 @@
"scripts": {
"lint": "eslint src tests",
"lint-fix": "npm run lint -- --fix",
"lint:schemas": "ajv compile -s schemas/json-logic.json -r 'schemas/**/*.json'",
"test": "cross-env NODE_ENV=test qunit 'tests/**/*.js' -r tap",
"pretest": "npm run build-package",
"build": "cross-env NODE_ENV=production npm run build-lib",
Expand Down
8 changes: 8 additions & 0 deletions schemas/common/all-types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/common/all-types.json",

"title": "All",
"description": "Any valid JSON data type.",
"type": ["array", "boolean", "null", "number", "object", "string"]
Copy link
Author

@AndyOGo AndyOGo Mar 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this for hardcoded static values...
But it forces us to use anyOf instead of oneOf below 🤔

}
53 changes: 53 additions & 0 deletions schemas/common/any.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/common/any.json",

"title": "All",
"description": "Any valid JSON Logic data source.",

"anyOf": [
{ "$ref": "http://jsonlogic.com/schemas/operators/accessor/variable.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/accessor/missing.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/accessor/missing_some.json" },

{ "$ref": "http://jsonlogic.com/schemas/operators/arithmetic/add.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/arithmetic/divide.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/arithmetic/modulo.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/arithmetic/multiply.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/arithmetic/substract.json" },

{ "$ref": "http://jsonlogic.com/schemas/operators/array/all.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/array/filter.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/array/map.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/array/merge.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/array/none.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/array/reduce.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/array/some.json" },

{ "$ref": "http://jsonlogic.com/schemas/operators/logic/and.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/logic/equal.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/logic/if.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/logic/not.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/logic/notEqual.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/logic/notnot.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/logic/or.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/logic/strictEqual.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/logic/strictNotEqual.json" },

{ "$ref": "http://jsonlogic.com/schemas/operators/misc/in.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/misc/log.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/misc/method.json" },

{ "$ref": "http://jsonlogic.com/schemas/operators/numeric/greater.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/numeric/greaterEqual.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/numeric/less.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/numeric/lessEqual.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/numeric/max.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/numeric/min.json" },

{ "$ref": "http://jsonlogic.com/schemas/operators/string/cat.json" },
{ "$ref": "http://jsonlogic.com/schemas/operators/string/substr.json" },

{ "$ref": "http://jsonlogic.com/schemas/common/all-types.json" }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This schema is very loose, mainly because the API itself is very forgiving

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case a forgiving APi is nice

]
}
25 changes: 25 additions & 0 deletions schemas/common/binary-args.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/common/binary-args.json",

"title": "Binary Arg",
"description": "Up to two args of valid JSON Logic data source.",

"oneOf": [
{
"title": "Array",
"description": "An array with one or two elements.",
"type": "array",
"minItems": 1,
"maxItems": 2,
"items": {
"$ref": "http://jsonlogic.com/schemas/common/any.json"
}
},
{
"$ref": "http://jsonlogic.com/schemas/common/any.json",
"title": "Single Arg",
"description": "Note: binary operators can also take a single, non array argument:"
}
]
}
24 changes: 24 additions & 0 deletions schemas/common/one-or-more-args.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/common/one-or-more-args.json",

"title": "1 or more args",
"description": "With 1 or more arguments.",

"oneOf": [
{
"title": "Single Array",
"description": "An array with 1 or more elements.",
"type": "array",
"minItems": 1,
"items": {
"$ref": "http://jsonlogic.com/schemas/common/any.json"
}
},
{
"$ref": "http://jsonlogic.com/schemas/common/any.json",
"title": "Single Arg",
"description": "Note: 1 or more operators can also take a single, non array argument:"
}
]
}
20 changes: 20 additions & 0 deletions schemas/common/pointer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/common/pointer.json",

"title": "Pointer",
"description": "Schema to access properties of an object or items of an array by index.",

"oneOf": [
{
"type": "string",
"title": "Property",
"description": "The key passed to var can use dot-notation to get the property of a property (to any depth you need):"
},
{
"type": "number",
"title": "Index",
"description": "You can also use the var operator to access an array by numeric index."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
]
}
25 changes: 25 additions & 0 deletions schemas/common/trinary-args.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/common/trinary-args.json",

"title": "Trinary Args",
"description": "Up to three args of valid JSON Logic data source.",

"oneOf": [
{
"title": "Array",
"description": "An array with one or three elements.",
"type": "array",
"minItems": 1,
"maxItems": 3,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it enough when we provide one-or-more args?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too at the beginning, then I saw that for example + and * take any number of arguments from 1 to infinity.
But - and / take only up to 2 - no more.

And then there are other special ops which take up to 3 like < and <= (in contrast to > and >=).

if is the most complex number of args, it 2n + 1, so 1, 3, 5, 7, 9, etc.

To make all of this even more complex, all operators support a single argument without an array.
It even doesn't matter if the provided number of arguments make sense, jsonLogic.apply will try it's best to never fail and output nonsense in the worst case 😅

"items": {
"$ref": "http://jsonlogic.com/schemas/common/any.json"
}
},
{
"$ref": "http://jsonlogic.com/schemas/common/any.json",
"title": "Single Arg",
"description": "Note: trinary operators can also take a single, non array argument:"
}
]
}
25 changes: 25 additions & 0 deletions schemas/common/unary-arg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/common/unary-arg.json",

"title": "Unary Arg",
"description": "Only one valid JSON Logic data source.",

"oneOf": [
{
"title": "Single Array",
"description": "An array with just one element.",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think unary and one-or-more args cover all constellations 🤔

"type": "array",
"minItems": 1,
"maxItems": 1,
"items": {
"$ref": "http://jsonlogic.com/schemas/common/any.json"
}
},
{
"$ref": "http://jsonlogic.com/schemas/common/any.json",
"title": "Single Arg",
"description": "Note: unary operators can also take a single, non array argument:"
}
]
}
32 changes: 32 additions & 0 deletions schemas/common/var.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/common/var.json",

"title": "Var",
"description": "Retrieve data from the provided data object.",

"oneOf": [
{
"type": "array",
"items": [
{ "$ref": "http://jsonlogic.com/schemas/common/pointer.json" },
{
"$ref": "http://jsonlogic.com/schemas/common/all-types.json",
"title": "Default",
"description": "You can supply a default, as the second argument, for values that might be missing in the data object."
}
]
},
{
"$ref": "http://jsonlogic.com/schemas/common/pointer.json",
"title": "Shortcut",
"description": "If you like, we support syntactic sugar to skip the array around single arguments."
},
{
"type": "string",
"enum": [""],
"title": "Entire data object",
"description": "You can also use var with an empty string to get the entire data object – which is really useful in map, filter, and reduce rules."
}
]
}
9 changes: 9 additions & 0 deletions schemas/json-logic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/json-logic.json",

"title": "JSON-Logic Schema",
"description": "Build complex rules, serialize them as JSON, share them between front-end and back-end.",

"$ref": "http://jsonlogic.com/schemas/common/any.json"
}
15 changes: 15 additions & 0 deletions schemas/operators/accessor/missing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/operators/accessor/missing.json",

"title": "missing",
"description": "Takes an array of data keys to search for (same format as var). Returns an array of any keys that are missing from the data object, or an empty array.",
"type": "object",
"additionalProperties": false,
"required": ["missing"],
"properties": {
"missing": {
"$ref": "http://jsonlogic.com/schemas/common/var.json"
}
}
}
28 changes: 28 additions & 0 deletions schemas/operators/accessor/missing_some.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/operators/accessor/missing_some.json",

"title": "missing_some",
"description": "Takes a minimum number of data keys that are required, and an array of keys to search for (same format as var or missing). Returns an empty array if the minimum is met, or an array of the missing keys otherwise.",
"type": "object",
"additionalProperties": false,
"required": ["missing_some"],
"properties": {
"missing_some": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"items": [
{
"type": "integer",
"title": "Need-Count",
"description": "Takes a minimum number of data keys that are required"
},
{
"$ref": "http://jsonlogic.com/schemas/common/var.json",
"description": "An array of keys to search for (same format as var or missing)."
}
]
}
}
}
15 changes: 15 additions & 0 deletions schemas/operators/accessor/variable.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this schema implements all features documented here:
http://jsonlogic.com/operations.html#var

"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/operators/accessor/variable.json",

"title": "var",
"description": "Retrieve data from the provided data object.",
"type": "object",
"additionalProperties": false,
"required": ["var"],
"properties": {
"var": {
"$ref": "http://jsonlogic.com/schemas/common/var.json"
}
}
}
16 changes: 16 additions & 0 deletions schemas/operators/arithmetic/add.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/operators/arithmetic/add.json",

"title": "+",
"description": "Addition. Because addition is associative, it happily take as many args as you want. Passing just one argument to + casts it to a number.",

"type": "object",
"additionalProperties": false,
"required": ["+"],
"properties": {
"+": {
"$ref": "http://jsonlogic.com/schemas/common/one-or-more-args.json"
}
}
}
16 changes: 16 additions & 0 deletions schemas/operators/arithmetic/divide.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/operators/arithmetic/divide.json",

"title": "/",
"description": "Division.",

"type": "object",
"additionalProperties": false,
"required": ["/"],
"properties": {
"/": {
"$ref": "http://jsonlogic.com/schemas/common/binary-args.json"
}
}
}
16 changes: 16 additions & 0 deletions schemas/operators/arithmetic/modulo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/operators/arithmetic/modulo.json",

"title": "%",
"description": "Module. Finds the remainder after the first argument is divided by the second argument.",

"type": "object",
"additionalProperties": false,
"required": ["%"],
"properties": {
"%": {
"$ref": "http://jsonlogic.com/schemas/common/binary-args.json"
}
}
}
16 changes: 16 additions & 0 deletions schemas/operators/arithmetic/multiply.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://jsonlogic.com/schemas/operators/arithmetic/multiply.json",

"title": "*",
"description": "Multiplication. Because multiplication is associative, it happily take as many args as you want.",

"type": "object",
"additionalProperties": false,
"required": ["*"],
"properties": {
"*": {
"$ref": "http://jsonlogic.com/schemas/common/one-or-more-args.json"
}
}
}
Loading