-
-
Notifications
You must be signed in to change notification settings - Fork 6
Split JSON rtti schemas into .f.mjs and JSON types into types.ts #1498
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8338590
iteration 1
sergey-shandar af7345e
Unknown
sergey-shandar e2a7df3
ok
sergey-shandar d30ee39
second
sergey-shandar 074b40a
second
sergey-shandar 6620128
3
sergey-shandar d676045
json/common
sergey-shandar f3a482d
4
sergey-shandar 7f0459a
common
sergey-shandar f2da8d5
rev
sergey-shandar 334cd91
fix
sergey-shandar 2c5cdaf
ok
sergey-shandar 3b12301
ok
sergey-shandar 5b0554c
readonly
sergey-shandar a681da6
Add CHANGELOG entry for the JSON rtti/types split
sergey-shandar cd5a10f
Document the typeof cross-reference rule for recursive constants
sergey-shandar 75bdaa2
Add @module header and co-located proof to media/json/rtti
sergey-shandar 6dbe384
Add @module header to media/json/types.ts
sergey-shandar cad89b0
Point docs at the new homes of the JSON types and schemas
sergey-shandar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * rtti schemas describing the JSON data model: `primitive`, `unknown`, | ||
| * `object`, and `array`. | ||
| * | ||
| * The three composite schemas are mutually recursive — `unknown` names | ||
| * `object` and `array`, both of which are built from `unknown` — so `unknown` | ||
| * carries an explicit `@type` that cross-references its neighbours through | ||
| * `typeof`. That spelling is deliberate: `@type {const}` also type-checks here, | ||
| * but leaves declaration emit no name for the recursive positions, so it | ||
| * inlines the structure, gives up at depth, and degrades the emitted `.d.mts` | ||
| * to `any`. See AGENTS.md §6.2. | ||
| * | ||
| * The TypeScript counterparts live in the sibling | ||
| * [`../types.ts`](../types.ts), which pins them against these schemas with | ||
| * `Assert<Equal<Unknown, Ts<typeof unknown>>>`. | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| import { | ||
| boolean as rttiBoolean, | ||
| number as rttiNumber, | ||
| string as rttiString, | ||
| or, | ||
| record, | ||
| array as rttiArray | ||
| } from '../../../types/rtti/module.f.mjs' | ||
|
|
||
| /** rtti schema matching any JSON primitive: `null`, `boolean`, `number`, or `string`. */ | ||
| export const primitive = or(null, rttiBoolean, rttiNumber, rttiString) | ||
|
|
||
| /** | ||
| * rtti schema matching any JSON value: a primitive, an array of JSON values, | ||
| * or an object whose values are JSON values. Self-referential via a thunk; | ||
| * rtti instantiates array/record item validators lazily so recursion terminates | ||
| * on acyclic input. | ||
| * | ||
| * A struct field typed `unknown` is **required when present** — unlike rtti | ||
| * core's `unknown`, the JSON `unknown` excludes `undefined`. | ||
| * | ||
| * @type {() => readonly['or', typeof primitive, typeof object, typeof array]} | ||
| */ | ||
| export const unknown = () => ['or', primitive, object, array] | ||
|
|
||
| /** | ||
| * rtti schema matching a JSON object: `{ readonly [k: string]?: Unknown }`. | ||
| */ | ||
| export const object = record(unknown) | ||
|
|
||
| /** rtti schema matching a JSON array: `readonly Unknown[]`. */ | ||
| export const array = rttiArray(unknown) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * Proof for the JSON rtti schemas. | ||
| * | ||
| * @module | ||
| */ | ||
|
|
||
| import { assertEq } from '../../../asserts/module.f.mjs' | ||
| import { validate } from '../../../types/rtti/validate/module.f.mjs' | ||
| import { primitive, unknown, object, array } from './module.f.mjs' | ||
|
|
||
| /** @import { ValidateE } from '../../../types/rtti/common/types.ts' */ | ||
| /** @import { Unknown } from '../../../types/rtti/ts/types.ts' */ | ||
|
|
||
| // Reduces a validation to its `ok`/`error` tag: these schemas are about what is | ||
| // accepted, not about the payload, which validation returns unchanged. The | ||
| // erased `ValidateE` keeps the shared helper from re-instantiating each | ||
| // schema's deep recursive result type (TS2589). | ||
| /** @type {(v: ValidateE) => (value: Unknown) => string} */ | ||
| const tag = v => value => v(value)[0] | ||
|
|
||
| const primitiveAccepts = tag(validate(primitive)) | ||
| const unknownAccepts = tag(validate(unknown)) | ||
| const objectAccepts = tag(validate(object)) | ||
| const arrayAccepts = tag(validate(array)) | ||
|
|
||
| export const proof = { | ||
| primitive: { | ||
| accepts: () => { | ||
| assertEq(primitiveAccepts(null), 'ok') | ||
| assertEq(primitiveAccepts(true), 'ok') | ||
| assertEq(primitiveAccepts(0), 'ok') | ||
| assertEq(primitiveAccepts(''), 'ok') | ||
| }, | ||
| rejectsComposite: () => { | ||
| assertEq(primitiveAccepts([]), 'error') | ||
| assertEq(primitiveAccepts({}), 'error') | ||
| }, | ||
| }, | ||
| object: { | ||
| accepts: () => { | ||
| assertEq(objectAccepts({}), 'ok') | ||
| assertEq(objectAccepts({ a: 1, b: 'two', c: null }), 'ok') | ||
| }, | ||
| // A record schema descends into its values, so a bad leaf anywhere in | ||
| // the tree fails the whole object. | ||
| nested: () => { | ||
| assertEq(objectAccepts({ a: { b: [1, { c: null }] } }), 'ok') | ||
| }, | ||
| rejectsNonObject: () => { | ||
| assertEq(objectAccepts(1), 'error') | ||
| assertEq(objectAccepts([]), 'error') | ||
| }, | ||
| }, | ||
| array: { | ||
| accepts: () => { | ||
| assertEq(arrayAccepts([]), 'ok') | ||
| assertEq(arrayAccepts([null, true, 2, 'three']), 'ok') | ||
| }, | ||
| nested: () => { | ||
| assertEq(arrayAccepts([[{ a: [] }]]), 'ok') | ||
| }, | ||
| rejectsNonArray: () => { | ||
| assertEq(arrayAccepts(1), 'error') | ||
| assertEq(arrayAccepts({}), 'error') | ||
| }, | ||
| }, | ||
| unknown: { | ||
| // `unknown` is the union of the three above, so it accepts every arm. | ||
| acceptsEveryArm: () => { | ||
| assertEq(unknownAccepts(null), 'ok') | ||
| assertEq(unknownAccepts(false), 'ok') | ||
| assertEq(unknownAccepts(1), 'ok') | ||
| assertEq(unknownAccepts('s'), 'ok') | ||
| assertEq(unknownAccepts([]), 'ok') | ||
| assertEq(unknownAccepts({}), 'ok') | ||
| }, | ||
| // The thunk closes the recursion: an arbitrarily deep acyclic value | ||
| // terminates because array/record item validators instantiate lazily. | ||
| recurses: () => { | ||
| assertEq(unknownAccepts({ a: [{ b: [[{ c: 'd' }]] }] }), 'ok') | ||
| }, | ||
| rejectsUndefined: () => { | ||
| assertEq(unknownAccepts(undefined), 'error') | ||
| }, | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
After moving the types and schemas out of this module, its header still advertises “JSON value types, rtti schemas” and says those schemas are defined here. The active design in
fjs/djs/todo/663-json-djs-tree-type.md:45-60,74-75likewise importsPrimitivefrom the removed location and requires the aliases to remain there, whilefjs/media/html/todo/665-json-html.md:90still saysUnknownlives there. These instructions now direct future work to a nonexistent API, so update the module JSDoc and dependent todo designs to usetypes.ts/rtti/module.f.mjs.AGENTS.md reference: AGENTS.md:L337-L339
Useful? React with 👍 / 👎.