From ecb0e989384e457fa354e423214ce0cb4a33dda5 Mon Sep 17 00:00:00 2001 From: paradoxuum Date: Tue, 24 Sep 2024 16:39:25 +0100 Subject: [PATCH] feat!: remove `validate` property from argument types This property served no purpose, as all of the actual validation/error handling is done in the type's transform function. --- .../docs/guides/types/defining-types.mdx | 8 ++--- docs/src/content/docs/reference/types.mdx | 11 ------- .../core/src/shared/builtin/types/color.ts | 9 ------ .../core/src/shared/builtin/types/duration.ts | 2 -- .../core/src/shared/builtin/types/players.ts | 4 --- .../src/shared/builtin/types/primitives.ts | 4 --- .../core/src/shared/builtin/types/team.ts | 2 -- packages/core/src/shared/core/registry.ts | 1 - packages/core/src/shared/core/type.ts | 31 ------------------- packages/core/src/shared/types.ts | 3 -- 10 files changed, 2 insertions(+), 73 deletions(-) diff --git a/docs/src/content/docs/guides/types/defining-types.mdx b/docs/src/content/docs/guides/types/defining-types.mdx index 9f89c8ef..98befc75 100644 --- a/docs/src/content/docs/guides/types/defining-types.mdx +++ b/docs/src/content/docs/guides/types/defining-types.mdx @@ -16,7 +16,6 @@ import { TransformResult, TypeBuilder } from "@rbxts/centurion"; import { t } from "@rbxts/t"; const playerType = TypeBuilder.create("player") - .validate(t.instanceOf("Player")) .transform((text, executor) => { if (text === "@me") { return TransformResult.ok(executor); @@ -39,10 +38,7 @@ for an example. ## Methods -- **validate**: Requires a type guard. You can use `@rbxts/t` for this, or you can define -them manually. - -- **transform**: Requires a function that takes the text and the executor +- **transform**: Takes a function that receives the argument text and the executor of the command and returns a `TransformResult`. -- **suggestions**: Optional, requires a function that returns an array of strings. \ No newline at end of file +- **suggestions**: Optional, takes a function that returns an array of strings. \ No newline at end of file diff --git a/docs/src/content/docs/reference/types.mdx b/docs/src/content/docs/reference/types.mdx index a2683b3c..90324493 100644 --- a/docs/src/content/docs/reference/types.mdx +++ b/docs/src/content/docs/reference/types.mdx @@ -7,7 +7,6 @@ title: Types - name: `string` - The name of the type. - expensive: `boolean` - Whether the type's transformation function is expensive. If `true`, type-checking will be disabled in the interface. -- validate: `(value) => boolean` - A function that validates a string as the type. - transform: `(text, executor) => TransformResult.Object` - A function that transforms a string into the type. - suggestions: `(text, executor) => string[]` - A function that returns suggestions @@ -39,16 +38,6 @@ from the provided type. **Returns**: A `TypeBuilder` instance. -### `validate(fn)` - -Sets the validation function for the type. - -**Parameters**: -- `fn` - The validation function. - -**Returns**: -The `TypeBuilder` instance. - ### `transform(fn, expensive)` Sets the transformation function for the type. diff --git a/packages/core/src/shared/builtin/types/color.ts b/packages/core/src/shared/builtin/types/color.ts index 5fcef414..b059eb5c 100644 --- a/packages/core/src/shared/builtin/types/color.ts +++ b/packages/core/src/shared/builtin/types/color.ts @@ -1,4 +1,3 @@ -import { t } from "@rbxts/t"; import { CenturionType } from "."; import { BaseRegistry, TransformResult, TypeBuilder } from "../../core"; @@ -216,7 +215,6 @@ const brickColorNames = new Set([ const brickColorNameArray = [...brickColorNames]; const brickColorType = TypeBuilder.create(CenturionType.BrickColor) - .validate(t.BrickColor) .transform((text) => { if (!brickColorNames.has(text)) { return TransformResult.err(`Invalid BrickColor: ${text}`); @@ -229,14 +227,7 @@ const brickColorType = TypeBuilder.create(CenturionType.BrickColor) .build(); const HEX_COLOR_PATTERN = "^#?%x%x%x%x%x%x$"; - -function isHexColor(value: unknown): value is Color3 { - if (!typeIs(value, "string")) return false; - return !value.match(HEX_COLOR_PATTERN).isEmpty(); -} - const hexColorType = TypeBuilder.create(CenturionType.HexColor) - .validate(isHexColor) .transform((text) => { if (text.match(HEX_COLOR_PATTERN).isEmpty()) { return TransformResult.err(`Invalid hex code: ${text}`); diff --git a/packages/core/src/shared/builtin/types/duration.ts b/packages/core/src/shared/builtin/types/duration.ts index 320ff19e..dd5a5aad 100644 --- a/packages/core/src/shared/builtin/types/duration.ts +++ b/packages/core/src/shared/builtin/types/duration.ts @@ -1,4 +1,3 @@ -import { t } from "@rbxts/t"; import { CenturionType } from "."; import { BaseRegistry, TransformResult, TypeBuilder } from "../../core"; @@ -32,7 +31,6 @@ assignUnit("month", ["mo", "mos", "months"]); assignUnit("year", ["y", "yr", "yrs", "years"]); const durationType = TypeBuilder.create(CenturionType.Duration) - .validate(t.integer) .transform((value: string) => { if (value === "0") return TransformResult.ok(0); diff --git a/packages/core/src/shared/builtin/types/players.ts b/packages/core/src/shared/builtin/types/players.ts index b617c2e1..fe8ea089 100644 --- a/packages/core/src/shared/builtin/types/players.ts +++ b/packages/core/src/shared/builtin/types/players.ts @@ -1,5 +1,4 @@ import { Players } from "@rbxts/services"; -import { t } from "@rbxts/t"; import { CenturionType } from "."; import { BaseRegistry, @@ -36,15 +35,12 @@ const getPlayerSuggestions = () => [ ...Players.GetPlayers().map((player) => player.Name), ]; -const isPlayer = t.instanceOf("Player"); const playerType = TypeBuilder.create(CenturionType.Player) - .validate(isPlayer) .transform(getPlayer) .suggestions(getPlayerSuggestions) .build(); const playersType = ListTypeBuilder.create(CenturionType.Players) - .validate(t.array(isPlayer)) .transform((input, executor) => { const includedPlayers = new Set(); let players: Player[] = []; diff --git a/packages/core/src/shared/builtin/types/primitives.ts b/packages/core/src/shared/builtin/types/primitives.ts index b8647c78..9ae6c18a 100644 --- a/packages/core/src/shared/builtin/types/primitives.ts +++ b/packages/core/src/shared/builtin/types/primitives.ts @@ -12,17 +12,14 @@ const transformToNumber = (text: string) => { }; const stringType = TypeBuilder.create(CenturionType.String) - .validate(t.string) .transform((text) => TransformResult.ok(text)) .build(); const numberType = TypeBuilder.create(CenturionType.Number) - .validate(t.number) .transform(transformToNumber) .build(); const integerType = TypeBuilder.create(CenturionType.Integer) - .validate(t.integer) .transform((text) => { const numResult = transformToNumber(text); if (!numResult.ok) { @@ -39,7 +36,6 @@ const integerType = TypeBuilder.create(CenturionType.Integer) const truthyValues = new Set(["true", "yes", "y"]); const falsyValues = new Set(["false", "no", "n"]); const booleanType = TypeBuilder.create(CenturionType.Boolean) - .validate(t.boolean) .transform((text) => { const textLower = text.lower(); if (truthyValues.has(textLower)) return TransformResult.ok(true); diff --git a/packages/core/src/shared/builtin/types/team.ts b/packages/core/src/shared/builtin/types/team.ts index 31fb1cb3..653412c2 100644 --- a/packages/core/src/shared/builtin/types/team.ts +++ b/packages/core/src/shared/builtin/types/team.ts @@ -1,10 +1,8 @@ import { Teams } from "@rbxts/services"; -import { t } from "@rbxts/t"; import { CenturionType } from "."; import { BaseRegistry, TransformResult, TypeBuilder } from "../../core"; const teamType = TypeBuilder.create(CenturionType.Team) - .validate(t.instanceOf("Team")) .transform((text) => { const team = Teams.FindFirstChild(text); if (team === undefined || !classIs(team, "Team")) { diff --git a/packages/core/src/shared/core/registry.ts b/packages/core/src/shared/core/registry.ts index 5e3a2503..b711aa6f 100644 --- a/packages/core/src/shared/core/registry.ts +++ b/packages/core/src/shared/core/registry.ts @@ -26,7 +26,6 @@ type Constructor = new (...args: never[]) => object; const argTypeSchema = t.interface({ name: t.string, expensive: t.boolean, - validate: t.callback, transform: t.callback, suggestions: t.optional(t.callback), }); diff --git a/packages/core/src/shared/core/type.ts b/packages/core/src/shared/core/type.ts index b27f4bcc..efb0de7c 100644 --- a/packages/core/src/shared/core/type.ts +++ b/packages/core/src/shared/core/type.ts @@ -1,4 +1,3 @@ -import { t } from "@rbxts/t"; import { ListArgumentType, SingleArgumentType } from "../types"; import { DecoratorMetadata, MetadataKey } from "./metadata"; @@ -38,7 +37,6 @@ export namespace TransformResult { export class TypeBuilder { private expensive = false; private marked = false; - private validationFn?: t.check; private transformFn?: SingleArgumentType["transform"]; private suggestionFn?: SingleArgumentType["suggestions"]; @@ -66,23 +64,11 @@ export class TypeBuilder { static extend(name: string, argumentType: SingleArgumentType) { const builder = new TypeBuilder(name); builder.expensive = argumentType.expensive; - builder.validationFn = argumentType.validate; builder.transformFn = argumentType.transform; builder.suggestionFn = argumentType.suggestions; return builder; } - /** - * Sets the validation function for this type. - * - * @param fn - The validation function. - * @returns The {@link TypeBuilder} instance. - */ - validate(fn: t.check) { - this.validationFn = fn; - return this; - } - /** * Sets the transformation function for this type. * @@ -133,14 +119,12 @@ export class TypeBuilder { * @returns A {@link SingleArgumentType} object. */ build(): SingleArgumentType { - assert(this.validationFn !== undefined, "Validation function is required"); assert(this.transformFn !== undefined, "Transform function is required"); const argType = { kind: "single", name: this.name, expensive: this.expensive, - validate: this.validationFn, transform: this.transformFn, suggestions: this.suggestionFn, } satisfies SingleArgumentType; @@ -159,7 +143,6 @@ export class TypeBuilder { export class ListTypeBuilder { private expensive = false; private marked = false; - private validationFn?: t.check; private transformFn?: ListArgumentType["transform"]; private suggestionFn?: ListArgumentType["suggestions"]; @@ -190,23 +173,11 @@ export class ListTypeBuilder { ) { const builder = new ListTypeBuilder(name); builder.expensive = argumentType.expensive; - builder.validationFn = argumentType.validate; builder.transformFn = argumentType.transform; builder.suggestionFn = argumentType.suggestions; return builder; } - /** - * Sets the validation function for this type. - * - * @param fn - The validation function. - * @returns The {@link ListTypeBuilder} instance. - */ - validate(fn: t.check) { - this.validationFn = fn; - return this; - } - /** * Sets the transformation function for this type. * @@ -257,14 +228,12 @@ export class ListTypeBuilder { * @returns A {@link ListArgumentType} object. */ build(): ListArgumentType { - assert(this.validationFn !== undefined, "Validation function is required"); assert(this.transformFn !== undefined, "Transform function is required"); const argType = { kind: "list", name: this.name, expensive: this.expensive, - validate: this.validationFn, transform: this.transformFn, suggestions: this.suggestionFn, } satisfies ListArgumentType; diff --git a/packages/core/src/shared/types.ts b/packages/core/src/shared/types.ts index 2cabe08d..12ea5d04 100644 --- a/packages/core/src/shared/types.ts +++ b/packages/core/src/shared/types.ts @@ -1,4 +1,3 @@ -import { t } from "@rbxts/t"; import { CommandContext, TransformResult } from "./core"; import { CenturionLogLevel } from "./util"; @@ -32,7 +31,6 @@ export interface SingleArgumentType { kind: "single"; name: string; expensive: boolean; - validate: t.check; transform: (text: string, executor: Player) => TransformResult.Object; suggestions?: (text: string, executor: Player) => string[]; } @@ -41,7 +39,6 @@ export interface ListArgumentType { kind: "list"; name: string; expensive: boolean; - validate: t.check; transform: (input: string[], executor: Player) => TransformResult.Object; suggestions?: (input: string[], executor: Player) => string[]; }