@amritk/generate-examples 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 amritk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -83,19 +83,24 @@ const integerExpr = (schema) => {
83
83
  const opts = [];
84
84
  // With both `minimum` and `exclusiveMinimum` present the effective lower bound
85
85
  // is the tighter (larger) of the two, so combine them rather than letting one
86
- // shadow the other via else-if.
86
+ // shadow the other via else-if. `fc.integer` also requires integral bounds, but
87
+ // the schema keywords may be fractional (`minimum: 2.5`, `exclusiveMinimum: 5.5`),
88
+ // so round each toward the satisfiable side: the smallest integer that still
89
+ // meets the lower bound, and the largest that still meets the upper.
90
+ // `Math.floor(x) + 1` is the smallest integer strictly greater than `x` (so it
91
+ // also handles integral exclusives); `Math.ceil(x) - 1` is the largest strictly less.
87
92
  const mins = [];
88
93
  if (hasMinimum(schema))
89
- mins.push(Number(schema.minimum));
94
+ mins.push(Math.ceil(Number(schema.minimum)));
90
95
  if (hasExclusiveMinimum(schema))
91
- mins.push(Number(schema.exclusiveMinimum) + 1);
96
+ mins.push(Math.floor(Number(schema.exclusiveMinimum)) + 1);
92
97
  if (mins.length > 0)
93
98
  opts.push(`min: ${Math.max(...mins)}`);
94
99
  const maxs = [];
95
100
  if (hasMaximum(schema))
96
- maxs.push(Number(schema.maximum));
101
+ maxs.push(Math.floor(Number(schema.maximum)));
97
102
  if (hasExclusiveMaximum(schema))
98
- maxs.push(Number(schema.exclusiveMaximum) - 1);
103
+ maxs.push(Math.ceil(Number(schema.exclusiveMaximum)) - 1);
99
104
  if (maxs.length > 0)
100
105
  opts.push(`max: ${Math.min(...maxs)}`);
101
106
  const base = opts.length > 0 ? `fc.integer({ ${opts.join(', ')} })` : 'fc.integer()';
@@ -173,14 +178,24 @@ const numberExpr = (schema) => {
173
178
  if (hasMultipleOf(schema) && schema.multipleOf > 0)
174
179
  return numberMultipleOfExpr(schema);
175
180
  const opts = ['noNaN: true', 'noDefaultInfinity: true'];
176
- if (hasMinimum(schema))
181
+ // With both an inclusive and an exclusive bound on the same side, honour the
182
+ // tighter one instead of letting `minimum`/`maximum` shadow the exclusive via
183
+ // else-if (which would emit values that violate the exclusive bound). The
184
+ // exclusive bound wins ties, since it additionally excludes the endpoint.
185
+ if (hasMinimum(schema) &&
186
+ (!hasExclusiveMinimum(schema) || Number(schema.minimum) > Number(schema.exclusiveMinimum))) {
177
187
  opts.push(`min: ${schema.minimum}`);
178
- else if (hasExclusiveMinimum(schema))
188
+ }
189
+ else if (hasExclusiveMinimum(schema)) {
179
190
  opts.push(`min: ${schema.exclusiveMinimum}`, 'minExcluded: true');
180
- if (hasMaximum(schema))
191
+ }
192
+ if (hasMaximum(schema) &&
193
+ (!hasExclusiveMaximum(schema) || Number(schema.maximum) < Number(schema.exclusiveMaximum))) {
181
194
  opts.push(`max: ${schema.maximum}`);
182
- else if (hasExclusiveMaximum(schema))
195
+ }
196
+ else if (hasExclusiveMaximum(schema)) {
183
197
  opts.push(`max: ${schema.exclusiveMaximum}`, 'maxExcluded: true');
198
+ }
184
199
  return `fc.double({ ${opts.join(', ')} })`;
185
200
  };
186
201
  /** Builds a `fc.array(...)` / `fc.uniqueArray(...)` / `fc.tuple(...)` expression for an array schema. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/generate-examples",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "Generate fast-check arbitraries and example values from JSON Schemas.",
5
5
  "module": "./dist/index.js",
6
6
  "type": "module",
@@ -47,8 +47,8 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "json-schema-typed": "^8.0.1",
50
- "@amritk/helpers": "0.13.0",
51
- "@amritk/runtime-validators": "0.7.0"
50
+ "@amritk/helpers": "0.13.2",
51
+ "@amritk/runtime-validators": "0.7.2"
52
52
  },
53
53
  "devDependencies": {
54
54
  "ajv": "^8.17.1"