Skip to content

Commit

Permalink
Fix broken test
Browse files Browse the repository at this point in the history
  • Loading branch information
robknight committed Sep 9, 2024
1 parent 1f531d0 commit 19b8d04
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/test-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"module": "ES2020",
"moduleResolution": "Bundler",
"esModuleInterop": true,
"strict": true,
"sourceMap": true
},
"include": ["**/*.ts", "**/*.tsx"],
Expand Down
37 changes: 36 additions & 1 deletion packages/podspec/src/parse/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ export interface EntriesParseOptions<E extends EntriesSchema> {
tuples?: EntriesTupleSchema<E>[];
}

const VALID_ENTRY_SCHEMA_TYPES = [
"int",
"string",
"cryptographic",
"eddsa_pubkey",
"optional"
] as const;

type ValidEntrySchemaType = (typeof VALID_ENTRY_SCHEMA_TYPES)[number];

// Type assertion to ensure ValidEntrySchemaType matches EntrySchema["type"]
type AssertEntrySchemaType = ValidEntrySchemaType extends EntrySchema["type"]
? EntrySchema["type"] extends ValidEntrySchemaType
? true
: false
: false;

// This will cause a compile-time error if the types don't match
const _: AssertEntrySchemaType = true;

// Runtime check function
function isValidEntryType(type: string): type is EntrySchema["type"] {
return (VALID_ENTRY_SCHEMA_TYPES as readonly string[]).includes(type);
}

/**
* A specification for a set of entries.
*/
Expand All @@ -72,7 +97,17 @@ export class EntriesSpec<E extends EntriesSchema> {
*
* @param schema The schema to use for this set of entries.
*/
private constructor(public readonly schema: E) {}
private constructor(public readonly schema: E) {
for (const [name, entry] of Object.entries(schema)) {
const entryType =
entry.type === "optional" ? entry.innerType.type : entry.type;
if (!isValidEntryType(entryType)) {
throw new Error(
`Entry ${name} contains invalid entry type: ${entryType as string}`
);
}
}
}

/**
* Parse entries without throwing an exception.
Expand Down
1 change: 0 additions & 1 deletion packages/podspec/test/podspec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ function generateRandomHex(byteLength: number): string {
function generateKeyPair(): { privateKey: string; publicKey: string } {
const privateKey = generateRandomHex(32);
const publicKey = encodePublicKey(
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
derivePublicKey(decodePrivateKey(privateKey))
);
return { privateKey, publicKey };
Expand Down

0 comments on commit 19b8d04

Please sign in to comment.