How to use the @webiny/commodo.WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS function in @webiny/commodo

To help you get started, we’ve selected a few @webiny/commodo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webiny / webiny-js / packages / api-security / src / plugins / graphql / installResolvers / install.js View on Github external
} else {
            // Update user's data
            user.firstName = data.firstName;
            user.lastName = data.lastName;
            await user.save();
        }

        try {
            await authPlugin.createUser({ data: args.data, user, permanent: true }, context);
            result.authUser = true;
        } catch {
            // Update firstName/lastName, but do not touch the existing password
            await authPlugin.updateUser({ data: omit(args.data, ["password"]), user }, context);
        }
    } catch (e) {
        if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }

    return new Response(result);
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / installResolvers / install.js View on Github external
user.lastName = data.lastName;
            await user.save();
        }

        try {
            await authPlugin.createUser({ data: args.data, user, permanent: true }, context);
            result.authUser = true;
        } catch {
            // Update firstName/lastName, but do not touch the existing password
            await authPlugin.updateUser({ data: omit(args.data, ["password"]), user }, context);
        }
    } catch (e) {
        if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }

    return new Response(result);
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / userResolvers / createUser.js View on Github external
const authPlugin = context.plugins
            .byType("security-authentication-provider")
            .filter(pl => pl.hasOwnProperty("createUser"))
            .pop();

        try {
            await authPlugin.createUser({ data: args.data, user }, context);
        } catch {
            // If user already exists we don't do anything on the auth provider side.
        }
    } catch (e) {
        if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }
    return new Response(user);
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / userResolvers / createUser.js View on Github external
try {
        await user.populate(args.data).save();

        const authPlugin = context.plugins
            .byType("security-authentication-provider")
            .filter(pl => pl.hasOwnProperty("createUser"))
            .pop();

        try {
            await authPlugin.createUser({ data: args.data, user }, context);
        } catch {
            // If user already exists we don't do anything on the auth provider side.
        }
    } catch (e) {
        if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }
    return new Response(user);
};
github webiny / webiny-js / packages / commodo-graphql / src / InvalidFieldsError.js View on Github external
each(invalidFields, ({ code, data, message }, name) => {
        if (code !== WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            return;
        }

        const path = prefix ? `${prefix}.${name}` : name;

        if (Array.isArray(data)) {
            return each(data, (err, index) => {
                const {
                    data: { invalidFields },
                    message
                } = err;
                if (!invalidFields) {
                    formatted[`${path}.${index}`] = message;
                    return;
                }
github webiny / webiny-js / packages / api-files / src / plugins / resolvers / updateFileBySrc.js View on Github external
export default async (root: any, args: Object, context: Object) => {
    const { File } = context.models;

    const model = await File.findOne({ query: { src: args.src } });
    if (!model) {
        return new NotFoundResponse();
    }

    try {
        await model.populate(args.data).save();
    } catch (e) {
        if (
            e instanceof WithFieldsError &&
            e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS
        ) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || "VALIDATION_FAILED_INVALID_FIELDS",
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data || null
        });
    }
    return new Response(model);
};
github webiny / webiny-js / packages / commodo-graphql / src / crudResolvers.js View on Github external
export const resolveCreate = (getModel: GetModelType) => async (
    root: any,
    args: Object,
    context: Object
) => {
    const Model = getModel(context);
    const model = new Model();

    try {
        await model.populate(args.data).save();
    } catch (e) {
        if (
            e instanceof WithFieldsError &&
            e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS
        ) {
            const fieldError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: fieldError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: fieldError.message,
                data: fieldError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }
    return new Response(model);
};
github webiny / webiny-js / packages / commodo-graphql / src / crudResolvers.js View on Github external
const model = await Model.findById(args.id);
    if (!model) {
        return notFound(args.id);
    }

    try {
        await model.populate(args.data);
        await model.save();
    } catch (e) {
        if (
            e instanceof WithFieldsError &&
            e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS
        ) {
            const fieldError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: fieldError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: fieldError.message,
                data: fieldError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data || null
        });
    }
    return new Response(model);
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / userResolvers / updateUser.js View on Github external
}

    try {
        await user.populate(data).save();

        const authPlugin = context.plugins
            .byType("security-authentication-provider")
            .filter(pl => pl.hasOwnProperty("updateUser"))
            .pop();

        await authPlugin.updateUser({ data: args.data, user }, context);
    } catch (e) {
        if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }
    return new Response(user);
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / userResolvers / updateUser.js View on Github external
if (!user) {
        return new NotFoundResponse(id ? `User "${id}" not found!` : "User not found!");
    }

    try {
        await user.populate(data).save();

        const authPlugin = context.plugins
            .byType("security-authentication-provider")
            .filter(pl => pl.hasOwnProperty("updateUser"))
            .pop();

        await authPlugin.updateUser({ data: args.data, user }, context);
    } catch (e) {
        if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }
    return new Response(user);
};