Skip to content

Commit 33d40cc

Browse files
authored
[fix wasm sampling] no more u64 (#34)
* no more u64 * fix python local dev
1 parent 0eb5390 commit 33d40cc

7 files changed

Lines changed: 57 additions & 23 deletions

File tree

Justfile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ check:
1313
# ---- Basic python smoke test ----
1414

1515
python-demo:
16-
@command -v maturin >/dev/null 2>&1 || (echo "error: maturin not found (install with 'pip install maturin' or 'cargo install maturin --locked')" >&2 && exit 1)
17-
@echo "[just] building Python extension via maturin …"
18-
maturin develop -q -m python/Cargo.toml
19-
@echo "[just] running Python demo …"
20-
@python examples/python/basic/demo.py
16+
uv run --reinstall-package jsoncompat examples/python/basic/demo.py
2117

2218
# ---- Basic javascript smoke test ----
2319

examples/python/basic/demo.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# /// script
22
# requires-python = ">=3.12"
3-
# dependencies = [
4-
# "jsoncompat==0.1.8a9",
5-
# ]
3+
# dependencies = ["jsoncompat"]
4+
# [tool.uv.sources]
5+
# jsoncompat = { path = "../../../python", editable = true }
66
# ///
77
"""End-to-end demo for the Python bindings of jsoncompat.
88
@@ -17,8 +17,21 @@
1717

1818

1919
def main() -> None:
20-
old_schema = '{"type": "string"}'
21-
new_schema = '{"type": "number"}'
20+
new_schema = """{
21+
"type": "object",
22+
"properties": {
23+
"name": { "type": "string", "minLength": 5 },
24+
"age": { "type": "integer", "minimum": 18 }
25+
},
26+
"required": ["name"]
27+
}"""
28+
old_schema = """{
29+
"type": "object",
30+
"properties": {
31+
"name": { "type": "string", "minLength": 5 },
32+
"age": { "type": "integer", "minimum": 18 }
33+
}
34+
}"""
2235

2336
print("=== Compatibility checks ===")
2437
roles: list[jsc.RoleLiteral] = [

examples/wasm/demo.html

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,40 @@
1010
</head>
1111
<body>
1212
<h1>json_schema_wasm - browser demo</h1>
13-
<pre id="out">initialising</pre>
13+
<pre id="out">initializing</pre>
1414

1515
<script type="module">
1616
// The Wasm package is built into `pkg/` by `just wasm-demo` with
1717
// wasm-pack build wasm --target web --out-dir examples/wasm/pkg
18-
import init, { check_compat, generate_value } from "../../wasm/pkg/jsoncompat_wasm.js";
18+
import init, { check_compat, generate_value } from "../../wasm/pkg/jsoncompat_wasm.js";
1919

2020
const out = document.getElementById('out');
2121

2222
(async () => {
2323
await init();
24+
const oldSchema = `{
25+
"type": "object",
26+
"properties": {
27+
"name": { "type": "string" },
28+
"age": { "type": "integer", "minimum": 18 }
29+
},
30+
"required": ["name"]
31+
}`;
32+
const newSchema = `{
33+
"type": "object",
34+
"properties": {
35+
"name": { "type": "string", "minLength": 5 },
36+
"age": { "type": "integer", "minimum": 18 }
37+
}
38+
}`;
2439

25-
const oldSchema = '{"type":"string"}';
26-
const newSchema = '{"type":"number"}';
2740

2841
const lines = [];
2942
lines.push('=== Compatibility checks ===');
3043
for (const role of ['serializer','deserializer','both']) {
3144
const ok = await check_compat(oldSchema, newSchema, role);
3245
lines.push(`${role.padEnd(12)}: ${ok}`);
3346
}
34-
3547
lines.push('\n=== Example value generation ===');
3648
const sample = await generate_value(oldSchema, 3);
3749
lines.push(sample);

fuzz/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ pub fn generate_value(schema: &SchemaNode, rng: &mut impl Rng, depth: u8) -> Val
342342
}
343343

344344
// Determine the desired object size bounds if specified.
345-
let min_p: usize = min_properties.unwrap_or(0).try_into().unwrap();
346-
let max_p: usize = max_properties.unwrap_or(u64::MAX).try_into().unwrap();
345+
let min_p: usize = min_properties.unwrap_or(0);
346+
let max_p: usize = max_properties.unwrap_or(usize::MAX);
347347

348348
// Random extra properties if allowed and we have not reached min_properties.
349349

python/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
publish = false # This crate is not published to crates.io; only PyPI.
77

88
[lib]
9-
name = "jsoncompat_py"
9+
name = "jsoncompat"
1010
crate-type = ["cdylib"]
1111

1212
[dependencies]

schema/src/ast.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ pub enum SchemaNode {
5050
additional: Box<SchemaNode>,
5151

5252
// Validation keywords for objects
53-
min_properties: Option<u64>,
54-
max_properties: Option<u64>,
53+
min_properties: Option<usize>,
54+
max_properties: Option<usize>,
5555
dependent_required: std::collections::HashMap<String, Vec<String>>,
5656

5757
enumeration: Option<Vec<Value>>,
@@ -809,8 +809,12 @@ fn parse_object_schema(obj: &serde_json::Map<String, Value>) -> Result<SchemaNod
809809
})
810810
.unwrap_or_default();
811811

812-
let min_properties = obj.get("minProperties").and_then(|v| v.as_u64());
813-
let max_properties = obj.get("maxProperties").and_then(|v| v.as_u64());
812+
let min_properties = obj
813+
.get("minProperties")
814+
.and_then(|v| v.as_u64().and_then(|n| usize::try_from(n).ok()));
815+
let max_properties = obj
816+
.get("maxProperties")
817+
.and_then(|v| v.as_u64().and_then(|n| usize::try_from(n).ok()));
814818

815819
Ok(SchemaNode::Object {
816820
properties,

web/jsoncompatdotcom/src/routes/fuzzer.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@ import wasmUrl from "jsoncompat/jsoncompat_wasm_bg.wasm?url";
55
import { useState } from "react";
66
import init, { generate_value } from "jsoncompat";
77

8+
const DEFAULT_SCHEMA = `{
9+
"type": "object",
10+
"properties": {
11+
"name": { "type": "string" },
12+
"age": { "type": "integer", "minimum": 18 }
13+
},
14+
"required": ["name"]
15+
}`;
16+
817
export const Route = createFileRoute("/fuzzer")({
918
component: FuzzerPage,
1019
});
1120

1221
function FuzzerPage() {
13-
const [schema, setSchema] = useState('{\n "type": "string"\n}');
22+
const [schema, setSchema] = useState(DEFAULT_SCHEMA);
1423
const [depth, setDepth] = useState(5);
1524
const [examples, setExamples] = useState<string[]>([]);
1625
const [numExamples, setNumExamples] = useState(5);

0 commit comments

Comments
 (0)