Skip to content

Commit

Permalink
refactor!: renamed validateSchema to buildSchema
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the function validateSchema is now buildSchema
  • Loading branch information
kpietraszko committed Jan 4, 2025
1 parent b0b53c1 commit 278d401
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { validateSchema, withIndex, InvalidSchemaError } from './indexedKeysSchema'
export { buildSchema, withIndex, InvalidSchemaError } from './indexedKeysSchema'
export { get, set } from "./raw";
export { toPlainObject, toIndexedKeysMessage } from "./convenient";

Expand Down
4 changes: 2 additions & 2 deletions src/indexedKeysSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type ValidIndexedKeysMessageSchema<TSchema> = {
[K in keyof TSchema]: TSchema[K] extends ValidSchemaLeaf<infer TField>
? ValidSchemaLeaf<TField>
: TSchema[K] extends SchemaLeaf<unknown>
? Invalid<"Schema needs to be validated before you use it, did you forget to call validateSchema()?">
? Invalid<"Schema needs to be built before you use it, did you forget to call buildSchema()?">
: ToValidIndexedKeysMessageSchema<TSchema[K]>;
};

Expand All @@ -54,7 +54,7 @@ export class InvalidSchemaError extends Error {
}
}

export function validateSchema<TSchema extends IndexedKeysMessageSchema<TSchemaInner>, TSchemaInner>(schema: TSchema) {
export function buildSchema<TSchema extends IndexedKeysMessageSchema<TSchemaInner>, TSchemaInner>(schema: TSchema) {
validateSchemaRecursively(schema, schema, [], 0);
return schema as unknown as ToValidIndexedKeysMessageSchema<TSchema>;
}
Expand Down
16 changes: 8 additions & 8 deletions test/indexedKeysSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
set,
toPlainObject,
toIndexedKeysMessage,
validateSchema,
buildSchema,
InvalidSchemaError
} from "../src/index";

Expand Down Expand Up @@ -222,18 +222,18 @@ describe("toIndexedKeysMessage", () => {
});
});

describe("validateSchema", () => {
it("shouldn't throw if schema is correct", () => {
describe("buildSchema", () => {
it("shouldn't throw if schema is valid", () => {
createTestSchema();
});

it("should throw if there are duplicate indexes-paths in the schema", () => {
expect(() => validateSchema({
expect(() => buildSchema({
someField: i(0)<number>(),
anotherFieldWithSameIndex: i(0)<number>(),
})).toThrowError(new InvalidSchemaError());

expect(() => validateSchema({
expect(() => buildSchema({
nestedThing: i(0)({
someNestedField: i(0)<string>()
}),
Expand Down Expand Up @@ -261,7 +261,7 @@ describe("validateSchema", () => {
someBool: i(3)<boolean>()
};

expect(() => validateSchema(schema))
expect(() => buildSchema(schema))
.toThrowError(new InvalidSchemaError());

const schema2 = {
Expand All @@ -281,7 +281,7 @@ describe("validateSchema", () => {
someBool: i(3)<boolean>()
};

expect(() => validateSchema(schema2))
expect(() => buildSchema(schema2))
.toThrowError(new InvalidSchemaError());
});
})
Expand All @@ -305,5 +305,5 @@ export function createTestSchema() {
someBool: i(3)<boolean>()
};

return validateSchema(schema);
return buildSchema(schema);
}

0 comments on commit 278d401

Please sign in to comment.