Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sandulat committed Nov 2, 2023
1 parent dd59eaf commit 0aa4797
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
27 changes: 16 additions & 11 deletions packages/basebuilder/src/builder-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,22 @@ async function validateEntityAttribute<TBuilder extends Builder>(
id: entityId,
};

await attribute.validate(attributeValue, {
schema,
entity: serializedEntity,
});

await (builder.entitiesExtensions as EntitiesExtensions)[
entity.type
]?.attributes?.[attributeName]?.validate?.(attributeValue, {
schema,
entity: serializedEntity,
});
const extensionValidator = (
builder.entitiesExtensions as EntitiesExtensions
)[entity.type]?.attributes?.[attributeName]?.validate;

if (extensionValidator) {
await extensionValidator?.(attributeValue, {
validate: attribute.validate,
schema,
entity: serializedEntity,
});
} else {
await attribute.validate(attributeValue, {
schema,
entity: serializedEntity,
});
}

delete entityAttributesErrors?.[
attributeName as keyof EntityAttributesErrors<TBuilder>
Expand Down
8 changes: 5 additions & 3 deletions packages/basebuilder/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ export type EntitiesExtensions<
{ name: K }
>["attributes"][number]["name"]]?: {
validate?: (
value: AttributesValues<
Extract<TEntities[number], { name: K }>["attributes"]
>[K2],
value: unknown,
context: {
schema: Schema<Builder<TEntities>>;
entity: SchemaEntityWithId<
Builder<[Extract<TEntities[number], { name: K }>]>
>;
validate: Extract<
Extract<TEntities[number], { name: K }>["attributes"][number],
{ name: K2 }
>["validate"];
},
) =>
| AttributesValues<
Expand Down
32 changes: 15 additions & 17 deletions packages/basebuilder/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,24 +313,22 @@ async function validateEntityAttributes(
try {
let attributeValue = entity.attributes[attribute.name];

attributeValue = await attribute.validate(attributeValue, {
schema: schema,
entity: {
...entity,
id: entity.id,
},
});

attributeValue =
(await (builder.entitiesExtensions as EntitiesExtensions)[
entity.type
]?.attributes?.[attribute.name]?.validate?.(attributeValue, {
const extensionValidator = (
builder.entitiesExtensions as EntitiesExtensions
)[entity.type]?.attributes?.[attribute.name]?.validate;

if (extensionValidator) {
attributeValue = await extensionValidator(attributeValue, {
validate: attribute.validate,
schema,
entity: {
...entity,
id: entity.id,
},
})) ?? attributeValue;
entity,
});
} else {
attributeValue = await attribute.validate(attributeValue, {
schema: schema,
entity,
});
}

newAttributes[attribute.name] = attributeValue;
} catch (error) {
Expand Down

0 comments on commit 0aa4797

Please sign in to comment.