From dcc27d1a933bb38463a041de3105812b565f569c Mon Sep 17 00:00:00 2001 From: Olivier Chafik Date: Sun, 9 Jun 2024 09:42:19 +0100 Subject: [PATCH] fix min in [1, 9] --- common/json-schema-to-grammar.cpp | 5 ++-- examples/json_schema_to_grammar.py | 5 ++-- .../server/public/json-schema-to-grammar.mjs | 5 ++-- tests/test-grammar-integration.cpp | 24 +++++++++++++++++++ tests/test-json-schema-to-grammar.cpp | 4 ++-- 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index b60966a2c..c2a574619 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -169,8 +169,9 @@ static void _build_min_max_int(int min_value, int max_value, std::stringstream & } } else if (min_value <= 9) { char c = '0' + min_value; - if (min_value > (top_level ? 1 : 0)) { - digit_range('0', c - 1); + auto range_start = top_level ? '1' : '0'; + if (c > range_start) { + digit_range(range_start, c - 1); out << " "; more_digits(1, less_decimals); out << " | "; diff --git a/examples/json_schema_to_grammar.py b/examples/json_schema_to_grammar.py index b1b7a974d..27a89596a 100755 --- a/examples/json_schema_to_grammar.py +++ b/examples/json_schema_to_grammar.py @@ -143,8 +143,9 @@ def _generate_min_max_int(min_value: Optional[int], max_value: Optional[int], ou more_digits(1, decimals_left) elif min_value <= 9: c = str(min_value) - if min_value > (1 if top_level else 0): - digit_range("0", chr(ord(c) - 1)) + range_start = '1' if top_level else '0' + if c > range_start: + digit_range(range_start, chr(ord(c) - 1)) out.append(" ") more_digits(1, less_decimals) out.append(" | ") diff --git a/examples/server/public/json-schema-to-grammar.mjs b/examples/server/public/json-schema-to-grammar.mjs index 7c0f9c7a2..3d3d2837c 100644 --- a/examples/server/public/json-schema-to-grammar.mjs +++ b/examples/server/public/json-schema-to-grammar.mjs @@ -165,8 +165,9 @@ function _generateMinMaxInt(minValue, maxValue, out, decimalsLeft = 16, topLevel } } else if (minValue <= 9) { const c = minValue.toString(); - if (minValue > (topLevel ? 1 : 0)) { - digitRange("0", String.fromCharCode(c.charCodeAt(0) - 1)); + const range_start = topLevel ? '1' : '0'; + if (c > range_start) { + digitRange(range_start, String.fromCharCode(c.charCodeAt(0) - 1)); out.push(" "); moreDigits(1, lessDecimals); out.push(" | "); diff --git a/tests/test-grammar-integration.cpp b/tests/test-grammar-integration.cpp index 0fe6abef1..51c0f6df0 100644 --- a/tests/test-grammar-integration.cpp +++ b/tests/test-grammar-integration.cpp @@ -153,6 +153,30 @@ static void test_simple_grammar() { "-0", } ); + test_schema( + "simple min 3", + R"""({ + "type": "integer", + "minimum": 3 + })""", + // Passing strings + { + "3", + "4", + "10", + "20", + }, + // Failing strings + { + "-1", + "-100", + "0", + "1", + "2", + "01", + "02", + } + ); test_schema( "simple min 456", R"""({ diff --git a/tests/test-json-schema-to-grammar.cpp b/tests/test-json-schema-to-grammar.cpp index 2134eac4b..93f74a57d 100755 --- a/tests/test-json-schema-to-grammar.cpp +++ b/tests/test-json-schema-to-grammar.cpp @@ -114,7 +114,7 @@ static void test_all(const std::string & lang, std::function