fix min in [1, 9]

This commit is contained in:
Olivier Chafik 2024-06-09 09:42:19 +01:00
parent a0f19047af
commit dcc27d1a93
5 changed files with 35 additions and 8 deletions

View file

@ -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 << " | ";

View file

@ -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(" | ")

View file

@ -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(" | ");

View file

@ -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"""({

View file

@ -114,7 +114,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"minimum": 3
})""",
R"""(
root ::= ([0-2] [0-9]{1,15} | [3-9] [0-9]{0,15}) space
root ::= ([1-2] [0-9]{1,15} | [3-9] [0-9]{0,15}) space
space ::= " "?
)"""
});
@ -127,7 +127,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"minimum": 9
})""",
R"""(
root ::= ([0-8] [0-9]{1,15} | [9] [0-9]{0,15}) space
root ::= ([1-8] [0-9]{1,15} | [9] [0-9]{0,15}) space
space ::= " "?
)"""
});