Skip to content

Commit 268bc8a

Browse files
committed
feat: add codec for basic schema types
0 parents  commit 268bc8a

File tree

14 files changed

+856
-0
lines changed

14 files changed

+856
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.psc-ide-port
2+
.spago
3+
output

.tidyrc.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"importSort": "ide",
3+
"importWrap": "auto",
4+
"indent": 2,
5+
"operatorsFile": null,
6+
"ribbon": 1,
7+
"typeArrowPlacement": "first",
8+
"unicode": "always",
9+
"width": 72
10+
}
11+

flake.lock

Lines changed: 115 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
description = "Logo language interpreter";
3+
4+
inputs = {
5+
easy-purescript-nix.url = "github:justinwoo/easy-purescript-nix/master";
6+
flake-utils.url = "github:numtide/flake-utils/main";
7+
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
8+
};
9+
10+
outputs =
11+
{ easy-purescript-nix
12+
, flake-utils
13+
, nixpkgs
14+
, ...
15+
}:
16+
let
17+
name = "purescript-json-schema";
18+
19+
supportedSystems = [
20+
"aarch64-darwin"
21+
"x86_64-linux"
22+
];
23+
24+
in
25+
flake-utils.lib.eachSystem supportedSystems (system:
26+
let
27+
pkgs = import nixpkgs {
28+
inherit system;
29+
};
30+
31+
easy-ps = import easy-purescript-nix { inherit pkgs; };
32+
33+
format-check = pkgs.stdenvNoCC.mkDerivation {
34+
checkPhase = ''
35+
purs-tidy check {src,test}
36+
'';
37+
doCheck = true;
38+
dontBuild = true;
39+
installPhase = ''
40+
mkdir "$out"
41+
'';
42+
name = "format-check";
43+
nativeBuildInputs = with easy-ps; [ purs-tidy ];
44+
src = ./.;
45+
};
46+
47+
devShellInputs = {
48+
easy-ps = with easy-ps; [
49+
psa
50+
purs
51+
purs-backend-es
52+
purs-tidy
53+
spago
54+
];
55+
56+
node-packages = with pkgs.nodePackages; [
57+
markdownlint-cli
58+
];
59+
60+
pkgs = with pkgs; [
61+
act
62+
docker
63+
esbuild
64+
gh
65+
git
66+
imagemagick
67+
nodejs
68+
];
69+
};
70+
in
71+
{
72+
checks = { inherit format-check; };
73+
devShell = pkgs.mkShell {
74+
inherit name;
75+
buildInputs =
76+
devShellInputs.easy-ps ++
77+
devShellInputs.node-packages ++
78+
devShellInputs.pkgs;
79+
PLAYWRIGHT_BROWSERS_PATH = "${pkgs.playwright-driver.browsers}";
80+
shellHook = ''
81+
PS1="\[\e[33m\][\[\e[m\]\[\e[34;40m\]${name}:\[\e[m\]\[\e[36m\]\w\[\e[m\]\[\e[33m\]]\[\e[m\]\[\e[32m\]\\$\[\e[m\] "
82+
'';
83+
};
84+
}
85+
);
86+
}

packages.dhall

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let upstream =
2+
https://github.com/purescript/package-sets/releases/download/psc-0.15.9-20230629/packages.dhall
3+
sha256:f91d36c7e4793fe4d7e042c57fef362ff3f9e9ba88454cd38686701e30bf545a
4+
5+
in upstream

spago.dhall

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{ name = "mlogo"
2+
, dependencies =
3+
[ "aff"
4+
, "argonaut-core"
5+
, "arrays"
6+
, "control"
7+
, "effect"
8+
, "either"
9+
, "exceptions"
10+
, "foldable-traversable"
11+
, "foreign-object"
12+
, "gen"
13+
, "lists"
14+
, "maybe"
15+
, "node-process"
16+
, "nonempty"
17+
, "ordered-collections"
18+
, "prelude"
19+
, "quickcheck"
20+
, "spec"
21+
, "strings"
22+
, "tailrec"
23+
, "tuples"
24+
]
25+
, packages = ./packages.dhall
26+
, sources = [ "src/purs/**/*.purs", "test/unit/**/*.purs" ]
27+
}

src/purs/JsonSchema.purs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
module JsonSchema
2+
( JsonArraySchemaSpec
3+
, JsonIntegerSchemaSpec
4+
, JsonNumberSchemaSpec
5+
, JsonObjectSchemaSpec
6+
, JsonSchemaObjectProperty
7+
, JsonSchema(..)
8+
, JsonStringSchemaSpec
9+
, genSchema
10+
) where
11+
12+
import Prelude
13+
14+
import Control.Lazy (class Lazy)
15+
import Control.Lazy as Lazy
16+
import Control.Monad.Gen (class MonadGen)
17+
import Control.Monad.Gen as Gen
18+
import Control.Monad.Gen.Common as GenCommon
19+
import Data.Generic.Rep (class Generic)
20+
import Data.List as List
21+
import Data.Map (Map)
22+
import Data.Map as Map
23+
import Data.Maybe (Maybe)
24+
import Data.NonEmpty ((:|))
25+
import Data.Show.Generic (genericShow)
26+
27+
data JsonSchema
28+
= JsonArraySchema JsonArraySchemaSpec
29+
| JsonBooleanSchema
30+
| JsonEmptySchema
31+
| JsonIntegerSchema JsonIntegerSchemaSpec
32+
| JsonNullSchema
33+
| JsonNumberSchema JsonNumberSchemaSpec
34+
| JsonObjectSchema JsonObjectSchemaSpec
35+
| JsonStringSchema JsonStringSchemaSpec
36+
37+
derive instance Generic JsonSchema _
38+
derive instance Eq JsonSchema
39+
40+
instance Show JsonSchema where
41+
show schema = genericShow schema
42+
43+
type JsonArraySchemaSpec =
44+
{ itemsSchema Maybe JsonSchema
45+
, uniqueItems Boolean
46+
}
47+
48+
type JsonBooleanSchemaSpec = {}
49+
50+
type JsonIntegerSchemaSpec = {}
51+
52+
type JsonNumberSchemaSpec = {}
53+
54+
type JsonStringSchemaSpec = {}
55+
56+
type JsonObjectSchemaSpec =
57+
{ properties Map String JsonSchemaObjectProperty
58+
}
59+
60+
type JsonSchemaObjectProperty =
61+
{ isRequired Boolean
62+
, schema JsonSchema
63+
}
64+
65+
genSchema m. Lazy (m JsonSchema) MonadGen m m JsonSchema
66+
genSchema = Lazy.defer \_ → Gen.oneOf $ genBooleanSchema :|
67+
[ genArraySchema
68+
, genEmptySchema
69+
, genIntegerSchema
70+
, genNullSchema
71+
, genNumberSchema
72+
, genObjectSchema
73+
, genStringSchema
74+
]
75+
76+
genArraySchema m. Lazy (m JsonSchema) MonadGen m m JsonSchema
77+
genArraySchema = do
78+
itemsSchema ← GenCommon.genMaybe genSchema
79+
uniqueItems ← Gen.chooseBool
80+
pure $ JsonArraySchema { itemsSchema, uniqueItems }
81+
82+
genBooleanSchema m. MonadGen m m JsonSchema
83+
genBooleanSchema = pure JsonBooleanSchema
84+
85+
genEmptySchema m. MonadGen m m JsonSchema
86+
genEmptySchema = pure JsonEmptySchema
87+
88+
genIntegerSchema m. MonadGen m m JsonSchema
89+
genIntegerSchema = pure $ JsonIntegerSchema {}
90+
91+
genNullSchema m. MonadGen m m JsonSchema
92+
genNullSchema = pure JsonNullSchema
93+
94+
genNumberSchema m. MonadGen m m JsonSchema
95+
genNumberSchema = pure $ JsonNumberSchema {}
96+
97+
genObjectSchema m. Lazy (m JsonSchema) MonadGen m m JsonSchema
98+
genObjectSchema = do
99+
properties ← genProperties
100+
pure $ JsonObjectSchema { properties }
101+
where
102+
genProperties m (Map String JsonSchemaObjectProperty)
103+
genProperties = (Map.fromFoldable <<< List.singleton)
104+
<$> GenCommon.genTuple (pure "prop") genProperty
105+
106+
genProperty m JsonSchemaObjectProperty
107+
genProperty = do
108+
isRequired ← Gen.chooseBool
109+
schema ← genSchema
110+
pure { isRequired, schema }
111+
112+
genStringSchema m. MonadGen m m JsonSchema
113+
genStringSchema = pure $ JsonStringSchema {}

0 commit comments

Comments
 (0)