@amritk/generate-examples 0.5.0 → 0.5.1

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.
@@ -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.1",
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.1",
51
+ "@amritk/runtime-validators": "0.7.1"
52
52
  },
53
53
  "devDependencies": {
54
54
  "ajv": "^8.17.1"