-
Notifications
You must be signed in to change notification settings - Fork 0
[rig-sampler] Add s.percent schema helper for percentage values (0–100) #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,7 @@ | |
| * T:JsonSchemaObject type {[key:string]:unknown} plain JSON Schema object | ||
| * s.string/number/integer/boolean/null SchemaHelperFactory primitives; call as value or fn(desc) | ||
| * s.int alias for s.integer; s.nonEmptyString string with minLength:1; s.url string with format:"uri"; s.path string with format:"path"; s.date string with format:"date" validated as YYYY-MM-DD | ||
| * s.positiveInt integer with minimum:1; s.nonNegativeInt integer with minimum:0; NumberSchema/IntegerSchema support minimum/maximum constraints | ||
| * s.positiveInt integer with minimum:1; s.nonNegativeInt integer with minimum:0; s.percent number with minimum:0,maximum:100; NumberSchema/IntegerSchema support minimum/maximum constraints | ||
| * s.array(items,desc?) ArraySchema; use for homogeneous lists, e.g. s.array(s.string) | ||
| * s.nonEmptyArray(items,desc?) ArraySchema with minItems:1; validates array has at least one element | ||
| * s.object(props,desc?) ObjectSchema; s.optional(inner) marks field optional; s.nullable(inner) accepts inner|null; use for fixed-key shapes | ||
|
|
@@ -253,6 +253,8 @@ export const s = { | |
| positiveInt: createConstrainedNumberSchema<IntegerSchema>({ type: "integer", minimum: 1 }), | ||
| /** Schema for a non-negative integer (minimum: 0). Call as `s.nonNegativeInt` or `s.nonNegativeInt("description")`. */ | ||
| nonNegativeInt: createConstrainedNumberSchema<IntegerSchema>({ type: "integer", minimum: 0 }), | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/codebase-design] The PR description explicitly identifies samples 41 and 43 as the motivation for 💡 What to updateIn sample 41 ( // Before
lines: s.number,
branches: s.number,
// After
lines: s.percent,
branches: s.percent,In sample 43 ( Additionally, samples 123, 143, and 153 each have |
||
| /** Schema for a percentage value (number in the range 0–100, inclusive). Use instead of `s.number` when the value is a percentage; validates that the number is between 0 and 100. Call as `s.percent` or `s.percent("description")`. */ | ||
| percent: createConstrainedNumberSchema<NumberSchema>({ type: "number", minimum: 0, maximum: 100 }), | ||
| /** Schema for a `boolean` value. Call as `s.boolean` or `s.boolean("description")`. */ | ||
| boolean: createTypedPrimitiveSchema<BooleanSchema>("boolean"), | ||
| /** Schema for the JSON `null` literal. Call as `s.null` or `s.null("description")`. */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1536,6 +1536,26 @@ describe("s.nonNegativeInt", () => { | |
| }); | ||
| }); | ||
|
|
||
| describe("s.percent", () => { | ||
| it("serializes to {type:'number', minimum:0, maximum:100}", () => { | ||
| expect(toJsonSchema(s.percent)).toEqual({ type: "number", minimum: 0, maximum: 100 }); | ||
| expect(toJsonSchema(s.percent("coverage percentage"))).toEqual({ type: "number", minimum: 0, maximum: 100, description: "coverage percentage" }); | ||
| }); | ||
|
|
||
| it("accepts numbers in range [0, 100]", () => { | ||
| expect(analyzeResponse(JSON.stringify(0), s.percent, "test", 1).ok).toBe(true); | ||
| expect(analyzeResponse(JSON.stringify(50.5), s.percent, "test", 1).ok).toBe(true); | ||
| expect(analyzeResponse(JSON.stringify(100), s.percent, "test", 1).ok).toBe(true); | ||
| }); | ||
|
|
||
| it("rejects numbers outside [0, 100]", () => { | ||
| const below = analyzeResponse(JSON.stringify(-1), s.percent, "test", 1); | ||
| expect(below.ok).toBe(false); | ||
| const above = analyzeResponse(JSON.stringify(101), s.percent, "test", 1); | ||
| expect(above.ok).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The rejection tests use integers only (-1, 101). Adding fractional near-boundary values would confirm the JSON Schema 💡 Suggested additionsexpect(analyzeResponse(JSON.stringify(100.001), s.percent, "test", 1).ok).toBe(false);
expect(analyzeResponse(JSON.stringify(-0.001), s.percent, "test", 1).ok).toBe(false);JSON Schema |
||
| describe("NumberSchema and IntegerSchema minimum/maximum", () => { | ||
| it("serializes minimum and maximum on number schema", () => { | ||
| const schema: import("rig").NumberSchema = { type: "number", minimum: 0, maximum: 1 }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/grill-with-docs] The header docstring and
rig.tsJSDoc were updated, butSKILL.md's schema helper table (around line 62) still lists onlys.numberand doesn't mentions.percent,s.positiveInt, ors.nonNegativeInt. Agents using SKILL.md as prompt context won't know to prefers.percentovers.numberfor percentage fields.💡 Suggested SKILL.md addition
In the schema helpers table, add a row or note like:
And in the prose guidance (around line 70), add: "Use
s.percentinstead ofs.numberfor percentage fields to enforce the 0–100 range."