Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions packages/format/src/describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,30 @@ export function describeSchema({
throw new Error("`pointer` option must start with '#'");
}

return referencesId(schema)
? describeSchemaById({ schema, ...(pointer ? { pointer } : {}) })
: referencesYaml(schema)
? describeSchemaByYaml({ schema, ...(pointer ? { pointer } : {}) })
: describeSchemaByObject({ schema, ...(pointer ? { pointer } : {}) });
const pointerOptions = pointer
? { pointer }
: {};

if (referencesId(schema)) {
return describeSchemaById({
schema: typeof schema === "object"
? schema
: { id: schema },
...pointerOptions
});
}

if (referencesYaml(schema)) {
return describeSchemaByYaml({
schema,
...pointerOptions
});
}

return describeSchemaByObject({
schema,
...pointerOptions
});
}

function describeSchemaById({
Expand Down Expand Up @@ -180,12 +199,15 @@ type NoExtraProperties<T, U extends T = T> =
export type SchemaPointer = `#${string}`;

export type SchemaReference =
| SchemaId
| SchemaById
| SchemaByYaml
| object /* JSONSchema object itself */;

export type SchemaId = string;

export type SchemaById = NoExtraProperties<{
id: string;
id: SchemaId;
}>;

export type SchemaByYaml = NoExtraProperties<{
Expand All @@ -194,12 +216,16 @@ export type SchemaByYaml = NoExtraProperties<{

export function referencesId(
schema: SchemaReference
): schema is SchemaById {
return Object.keys(schema).length === 1 && "id" in schema;
): schema is SchemaId | SchemaById {
return (
typeof schema === "string" ||
(Object.keys(schema).length === 1 && "id" in schema)
);
}

export function referencesYaml(
schema: SchemaReference
): schema is SchemaByYaml {
return Object.keys(schema).length === 1 && "yaml" in schema;
return typeof schema === "object" &&
Object.keys(schema).length === 1 && "yaml" in schema;
}
12 changes: 3 additions & 9 deletions packages/format/src/types/data/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@ import { Data } from "./index.js";
describe("type guards", () => {
const schemaGuards = [
{
schema: {
id: "schema:ethdebug/format/data/value"
},
schema: "schema:ethdebug/format/data/value",
guard: Data.isValue
},
{
schema: {
id: "schema:ethdebug/format/data/unsigned"
},
schema: "schema:ethdebug/format/data/unsigned",
guard: Data.isUnsigned
},
{
schema: {
id: "schema:ethdebug/format/data/hex"
},
schema: "schema:ethdebug/format/data/hex",
guard: Data.isHex
},
] as const;
Expand Down
22 changes: 6 additions & 16 deletions packages/format/src/types/materials/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,30 @@ import { Materials } from "./index";
describe("type guards", () => {
const schemaGuards = [
{
schema: {
id: "schema:ethdebug/format/materials/id"
},
schema: "schema:ethdebug/format/materials/id",
guard: Materials.isId
},
{
schema: {
id: "schema:ethdebug/format/materials/reference"
},
schema: "schema:ethdebug/format/materials/reference",
guard: Materials.isReference
},
{
schema: {
id: "schema:ethdebug/format/materials/compilation"
},
schema: "schema:ethdebug/format/materials/compilation",
guard: Materials.isCompilation
},
{
schema: {
id: "schema:ethdebug/format/materials/source"
},
schema: "schema:ethdebug/format/materials/source",
guard: Materials.isSource
},
{
schema: {
id: "schema:ethdebug/format/materials/source-range"
},
schema: "schema:ethdebug/format/materials/source-range",
guard: Materials.isSourceRange
},
] as const;

for (const { guard, ...describeSchemaOptions } of schemaGuards) {
const { schema } = describeSchemaOptions;
describe(schema.id.slice("schema:".length), () => {
describe(schema.slice("schema:".length), () => {
it("matches its examples", () => {
const {
schema: {
Expand Down
52 changes: 13 additions & 39 deletions packages/format/src/types/pointer/pointer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { describeSchema } from "../../describe";
import { Pointer, isPointer } from "./pointer";

describe("type guards", () => {
const expressionSchema = {
id: "schema:ethdebug/format/pointer/expression"
};
const expressionSchema = "schema:ethdebug/format/pointer/expression";

const schemaGuards = [
{
Expand Down Expand Up @@ -60,75 +58,51 @@ describe("type guards", () => {
guard: Pointer.Expression.isResize
},
{
schema: {
id: "schema:ethdebug/format/pointer/region"
},
schema: "schema:ethdebug/format/pointer/region",
guard: Pointer.isRegion
},
{
schema: {
id: "schema:ethdebug/format/pointer/region/stack"
},
schema: "schema:ethdebug/format/pointer/region/stack",
guard: Pointer.Region.isStack
},
{
schema: {
id: "schema:ethdebug/format/pointer/region/memory"
},
schema: "schema:ethdebug/format/pointer/region/memory",
guard: Pointer.Region.isMemory
},
{
schema: {
id: "schema:ethdebug/format/pointer/region/storage"
},
schema: "schema:ethdebug/format/pointer/region/storage",
guard: Pointer.Region.isStorage
},
{
schema: {
id: "schema:ethdebug/format/pointer/region/calldata"
},
schema: "schema:ethdebug/format/pointer/region/calldata",
guard: Pointer.Region.isCalldata
},
{
schema: {
id: "schema:ethdebug/format/pointer/region/returndata"
},
schema: "schema:ethdebug/format/pointer/region/returndata",
guard: Pointer.Region.isReturndata
},
{
schema: {
id: "schema:ethdebug/format/pointer/region/transient"
},
schema: "schema:ethdebug/format/pointer/region/transient",
guard: Pointer.Region.isTransient
},
{
schema: {
id: "schema:ethdebug/format/pointer/region/code"
},
schema: "schema:ethdebug/format/pointer/region/code",
guard: Pointer.Region.isCode
},
{
schema: {
id: "schema:ethdebug/format/pointer/collection/group"
},
schema: "schema:ethdebug/format/pointer/collection/group",
guard: Pointer.Collection.isGroup
},
{
schema: {
id: "schema:ethdebug/format/pointer/collection/list"
},
schema: "schema:ethdebug/format/pointer/collection/list",
guard: Pointer.Collection.isList
},
{
schema: {
id: "schema:ethdebug/format/pointer"
},
schema: "schema:ethdebug/format/pointer",
guard: isPointer
},
{
schema: {
id: "schema:ethdebug/format/pointer/template"
},
schema: "schema:ethdebug/format/pointer/template",
guard: Pointer.isTemplate
},
] as const;
Expand Down
18 changes: 5 additions & 13 deletions packages/format/src/types/program/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,26 @@ import { Context, isContext } from "./context";
describe("type guards", () => {
const schemaGuards = [
{
schema: {
id: "schema:ethdebug/format/program/context"
},
schema: "schema:ethdebug/format/program/context",
guard: isContext
},
{
schema: {
id: "schema:ethdebug/format/program/context/code"
},
schema: "schema:ethdebug/format/program/context/code",
guard: Context.isCode
},
{
schema: {
id: "schema:ethdebug/format/program/context/variables"
},
schema: "schema:ethdebug/format/program/context/variables",
guard: Context.isVariables
},
{
schema: {
id: "schema:ethdebug/format/program/context/remark"
},
schema: "schema:ethdebug/format/program/context/remark",
guard: Context.isRemark
},
] as const;

for (const { guard, ...describeSchemaOptions } of schemaGuards) {
const { schema } = describeSchemaOptions;
describe(schema.id.slice("schema:".length), () => {
describe(schema.slice("schema:".length), () => {
it("matches its examples", () => {
const {
schema: {
Expand Down
6 changes: 2 additions & 4 deletions packages/format/src/types/program/instruction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import { Instruction, isInstruction } from "./instruction";
describe("type guards", () => {
const schemaGuards = [
{
schema: {
id: "schema:ethdebug/format/program/instruction"
},
schema: "schema:ethdebug/format/program/instruction",
guard: isInstruction
},
] as const;

for (const { guard, ...describeSchemaOptions } of schemaGuards) {
const { schema } = describeSchemaOptions;
describe(schema.id.slice("schema:".length), () => {
describe(schema.slice("schema:".length), () => {
it("matches its examples", () => {
const {
schema: {
Expand Down
6 changes: 2 additions & 4 deletions packages/format/src/types/program/program.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import { Program, isProgram } from "./program";
describe("type guards", () => {
const schemaGuards = [
{
schema: {
id: "schema:ethdebug/format/program"
},
schema: "schema:ethdebug/format/program",
guard: isProgram
},
] as const;

for (const { guard, ...describeSchemaOptions } of schemaGuards) {
const { schema } = describeSchemaOptions;
describe(schema.id.slice("schema:".length), () => {
describe(schema.slice("schema:".length), () => {
it("matches its examples", () => {
const {
schema: {
Expand Down
14 changes: 4 additions & 10 deletions packages/format/src/types/type/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,17 @@ import * as Base from "./base";
describe("type guards", () => {
const schemaGuards = [
{
schema: {
id: "schema:ethdebug/format/type/base"
},
schema: "schema:ethdebug/format/type/base",
pointer: "#/$defs/ElementaryType",
guard: Base.isElementary
},
{
schema: {
id: "schema:ethdebug/format/type/base"
},
schema: "schema:ethdebug/format/type/base",
pointer: "#/$defs/ComplexType",
guard: Base.isComplex
},
{
schema: {
id: "schema:ethdebug/format/type/base"
},
schema: "schema:ethdebug/format/type/base",
pointer: "#/$defs/TypeWrapper",
guard: Base.isWrapper
},
Expand All @@ -32,7 +26,7 @@ describe("type guards", () => {
for (const { guard, ...describeSchemaOptions } of schemaGuards) {
const { schema, pointer } = describeSchemaOptions;
describe(
`${schema.id.slice("schema:".length)}${pointer}`,
`${schema.slice("schema:".length)}${pointer}`,
() => {
it("matches its examples", () => {
const {
Expand Down
Loading