Giter Club home page Giter Club logo

Comments (9)

MrWolfZ avatar MrWolfZ commented on July 27, 2024

from ngrx-forms.

nihique avatar nihique commented on July 27, 2024

this is 'stripped out' snippet from our app, where we are validating invoice.payments elements (latter example from MrWolfZ answer):

export interface State {
    form: FormGroupState<Invoice>;
    dateTimeSettings: DateTimeSettings;
}

export interface Invoice extends Entity {
    payments: Payment[];
}

export interface Payment extends Entity {
    invoiceId: number;
    description: string | null;
    amount: number;
}

const FORM_NAME = 'INVOICE_DETAIL_FORM';

const initialForm = createForm(newInvoice(defaultDateTimeSettings), FORM_NAME);

export const initialState: State = {
    form: initialForm,
    dateTimeSettings: defaultDateTimeSettings,
};

const formReducerFn = (dateTimeSettings: DateTimeSettings) =>
    createFormGroupReducerWithUpdate<Invoice>({
        payments: updateArray<Payment>(
            updateGroup<Payment>({
                description: validate(customValidations.required),
                amount: validate([
                    customValidations.required,
                    customValidations.double,
                ]),
            }),
        ),
    });

from ngrx-forms.

philjones88 avatar philjones88 commented on July 27, 2024

Pretty much what @nihique said, validate the items in the array but also that there's at least 1 which I managed to figure out myself.

from ngrx-forms.

philjones88 avatar philjones88 commented on July 27, 2024

I've tried to copy your example and get:

[ts]
Argument of type '{ name: ProjectFn<AbstractControlState<string>>; blah: ProjectFn<AbstractControlState<string | any...' is not assignable to parameter of type 'FormGroupState<TemplateRole>'.
  Object literal may only specify known properties, and 'name' does not exist in type 'FormGroupState<TemplateRole>'.

with the model:

iterface Template {
  name: string;
  roles: TemplateRole[];
}

interface TemplateRole {
  name: string;
}

with code:

createFormGroupReducerWithUpdate<Template>({
  name: validate<string>(required),
  roles: updateArray<TemplateRole>(
    updateGroup<TemplateRole>({
      name: validate<string>(required)
    })
  )
});

from ngrx-forms.

MrWolfZ avatar MrWolfZ commented on July 27, 2024

Yes, the typing for those update functions is a bit sub-optimal. This should do the trick:

createFormGroupReducerWithUpdate<Template>({
  name: validate<string>(required),
  roles: roles => updateArray<TemplateRole>(
    cast(roles),
    updateGroup<TemplateRole>({
      name: validate<string>(required)
    }),
  )
});

With the next version (which I'll release as soon as TypeScript 2.8 is finally released) your version will work due to improved type inference for child controls. I will also clean up the update functions in general to be more consistent.

In regards to combining validation of array items with the whole array you would write the following:

createFormGroupReducerWithUpdate<Template>({
  name: validate<string>(required),
  roles: roles => {
    roles = validate<TemplateRole[]>(minLength(1), roles);
    return updateArray<TemplateRole>(
      cast(roles),
      updateGroup<TemplateRole>({
        name: validate<string>(required)
      }),
    );
  }
});

from ngrx-forms.

philjones88 avatar philjones88 commented on July 27, 2024

That works thanks.

But.

if you add another property to validate inside the updateArray, updateGroup or add another updateGroup you get the error again. I can't seem to find the right combination to validate multiple properties inside the array:(

from ngrx-forms.

MrWolfZ avatar MrWolfZ commented on July 27, 2024

Sorry for the late response. I hope you were able to find the right combination in the meantime. If not, to work around these typing issues (which I am in the process of fixing properly) you can always make the updateGroup or updateArray call explicit by providing an arrow function (Just as I did in my example for the updateArray call. Basically, in my previous example you would adjust the reducer like this:

createFormGroupReducerWithUpdate<Template>({
  name: validate<string>(required),
  roles: roles => {
    roles = validate<TemplateRole[]>(minLength(1), roles);
    return updateArray<TemplateRole>(
      cast(roles),
      // by making the `role` state parameter explicit we ensure that the
      // TypeScript compiler chooses the right overload
      role => updateGroup<TemplateRole>(cast(role), {
        name: validate<string>(required)
      }),
    );
  }
});

from ngrx-forms.

MrWolfZ avatar MrWolfZ commented on July 27, 2024

Alright, so in the new and shiny TypeScript 2.8 world the following will type check:

interface Template {
  name: string;
  roles: TemplateRole[];
}

interface TemplateRole {
  name: string;
}

createFormGroupReducerWithUpdate<Template>({
  name: validate(required),
  roles: compose(
    validate<TemplateRole[]>(minLength(1)),
    updateArray(
      updateGroup<TemplateRole>({
        name: validate(required),
      }),
    ),
  ),
});

As far as I know Angular plans to drop TypeScript 2.8 support sometime in May at which point I will also release version 3.0.0 of ngrx-forms. I'll close this issue once that happens.

from ngrx-forms.

MrWolfZ avatar MrWolfZ commented on July 27, 2024

I have just released version 3.0.0. The code above should now properly type check.

from ngrx-forms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.