Skip to content

Commit

Permalink
feat: add allowed property to SchemaEnumerated class
Browse files Browse the repository at this point in the history
This commit adds a new `allowed` property to the `SchemaEnumerated` class. The `allowed` property is now a readonly property that is passed as an argument to the constructor. This change allows for better encapsulation and simplifies the code by removing the need for a private `#allowed` field.
  • Loading branch information
adwher committed Sep 4, 2024
1 parent 6ba16d1 commit c628972
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
13 changes: 7 additions & 6 deletions schemas/enumerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ export class SchemaEnumerated<
T extends readonly Enumerable[],
> implements Schema<T[number]> {
readonly name = SCHEMA_ENUMERATED_NAME;
/** Allowed values of the schema. */
#allowed: T;

/**
* Creates a new enumerated schema that only receives the given `options`.
* @param allowed Allowed values of the schema.
*/
constructor(allowed: T) {
this.#allowed = allowed;
constructor(readonly allowed: T) {
}

check(value: unknown, context: Context): Check<T[number]> {
Expand All @@ -37,12 +34,16 @@ export class SchemaEnumerated<
return ISSUE_VALIDATION;
}

return failure({ reason: "VALIDATION", expected: this.#allowed });
return failure({
reason: "TYPE",
expected: this.allowed,
received: value,
});
}

/** Checks `value` is allowed in the `options`. */
canUse(value: unknown): value is T[number] {
return isEnumerable(value) && this.#allowed.includes(value);
return isEnumerable(value) && this.allowed.includes(value);
}
}

Expand Down
3 changes: 2 additions & 1 deletion schemas/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export class SchemaObject<S extends SchemaShape> implements SchemaFrom<S> {
const final = value;
const issues: Issue[] = [];

const keys = Object.keys(this.shape).concat(Object.keys(value));
// Merge keys from both the shape and the value.
const keys = new Set(Object.keys(this.shape).concat(Object.keys(value)));

for (const key of keys) {
const schema = this.shape[key];
Expand Down
9 changes: 5 additions & 4 deletions schemas/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ export const SCHEMA_TRANSFORM_NAME = "SCHEMA_TRANSFORM";
export class SchemaTransform<G, R> {
readonly name = SCHEMA_TRANSFORM_NAME;

#schema: Schema<G>;
#transformer: SchemaTransformer<G, R>;

/**
* Creates a new instance of the SchemaTransform class.
* @param schema The schema to validate the input value against.
* @param transformer The transformer function to apply to the validated value.
*/
constructor(schema: Schema<G>, transformer: SchemaTransformer<G, R>) {
this.#schema = schema;
constructor(
readonly schema: Schema<G>,
transformer: SchemaTransformer<G, R>,
) {
this.#transformer = transformer;
}

check(value: unknown, context: Context): Check<R> {
const commit = this.#schema.check(value, context);
const commit = this.schema.check(value, context);

if (commit === undefined) {
return success(this.#transformer(value as G, context));
Expand Down

0 comments on commit c628972

Please sign in to comment.