Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct pattern for optional 'required' properties? #251

Open
chr4ss12 opened this issue Apr 3, 2023 · 0 comments
Open

correct pattern for optional 'required' properties? #251

chr4ss12 opened this issue Apr 3, 2023 · 0 comments

Comments

@chr4ss12
Copy link

chr4ss12 commented Apr 3, 2023

given the following structure:

{
     onlyIfNeverHadPremiumBefore?: boolean;
     onlyBrandNewUsers?: boolean;
  
  params: {
     messageContentId?: string;
     titleContentId?: string;
     
     userId: string;
     template?: object;
     
     message?: string;
     title?: string;
  }

I would like to do conditional validation based on the fact that messageContentId&titleContentId should exist both at once, or title&message should exist,

I'll let the code do the talking:

 const commonParams = {
            onlyIfNeverHadPremiumBefore: ow.optional.boolean,
            onlyBrandNewUsers: ow.optional.boolean
        };

        const commonInsideParams = {
            userId: ow.string,
            template: ow.optional.object
        };

        ow(data, ow.any(
            ow.object.exactShape({
                ...commonParams,

                params: ow.object.exactShape({
                    textContentId: ow.string,
                    titleContentId: ow.string,

                    ...commonInsideParams
                })
            }),

            ow.object.exactShape({
                ...commonParams,

                params: ow.object.exactShape({
                    text: ow.string,
                    title: ow.string,

                    ...commonInsideParams
                }),

            })));

I wonder if there is a better way? this is pretty unreadable, I'd prefer something a long of lines:

    ow(data, 
        ow.object.exactShape({
            
            onlyIfNeverHadPremiumBefore: ow.optional.boolean,
            onlyBrandNewUsers: ow.optional.boolean,

            params: ow.object.exactShape({
                
                userId: ow.string,
                template: ow.optional.object,

                ...(ow.any(
                    ow.object.exactShape({
                        titleContentId: ow.string,
                        messageContentId: ow.string,
                    }),
                    
                    ow.object.exactShape({
                        title: ow.string,
                        message: ow.string,
                    }))
            })
        }),
    });
    ```




  
  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant