Skip to content

Initial data should be populated in builders, not in validators #94

Closed
@antmendoza

Description

@antmendoza

What would you like to be added:

I think that the initial/default object state should be builder responsibility.

The actual generated code is something like

export function eventdefValidator(data: Specification.Eventdef): (() => Specification.Eventdef) {
  return () => {
    data.kind =  data.kind || 'consumed';
    const validate = validators.get('Eventdef');
    // TODO: ignore validation if no validator or throw ?
    if (!validate) return data;
    if (!validate(data)) {
      console.warn(validate.errors);
      const firstError: DefinedError = (validate.errors as DefinedError[])[0];
      throw new Error(`Eventdef is invalid: ${firstError.message}`);
    }
    return data;
  };
}

export function eventdefBuilder(): Builder<Specification.Eventdef> {
  return builder<Specification.Eventdef>(eventdefValidator);
}

where the validator responsibility is both to validate and return the default state, in this case data.kind = data.kind || 'consumed';

data.kind: consumed should be set as part of the builder.

Why is this needed:

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions