UNPKG

@vaadin/hilla-models

Version:

Generative form models for Hilla

49 lines 1.75 kB
import { $assertSupportedModel, $defaultValue, $name, Model, } from './Model.js'; export class ConstraintBuilder { #supportedModel; [$name]; #attributeDefaults; constructor() { this.#supportedModel = Model; this.#attributeDefaults = {}; } model(supportedModel) { this.#supportedModel = supportedModel; return this; } name(name) { this[$name] = name; return this; } attribute(name, model) { this.#attributeDefaults[name] = model[$defaultValue]; return this; } build() { const name = this[$name]; const attributeDefaults = this.#attributeDefaults; const supportedModel = this.#supportedModel; function assertSupportedModel(model) { if (!(model instanceof supportedModel)) { throw new Error(`The constraint "${name}" is not applicable to the model "${model[$name]}".`); } } let NonAttributedConstraint = ((valueOrAttributes) => { const attributes = { ...attributeDefaults, ...(typeof valueOrAttributes === 'object' && valueOrAttributes !== null ? valueOrAttributes : { value: valueOrAttributes }), }; return Object.defineProperties(Object.create(NonAttributedConstraint), { attributes: { value: attributes }, }); }); NonAttributedConstraint = Object.defineProperties(NonAttributedConstraint, { name: { value: name }, [$assertSupportedModel]: { value: assertSupportedModel }, }); return NonAttributedConstraint; } } //# sourceMappingURL=ConstraintBuilder.js.map