How to use webiny-validation - 9 common examples

To help you get started, we’ve selected a few webiny-validation 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 / webiny-api-security / src / models / SecurityUser.model.js View on Github external
return value;
                }

                value = value.toLowerCase().trim();
                instance.registerHookCallback("beforeSave", async () => {
                    const existingUser = await getModel("SecurityUser").findOne({
                        query: { email: value }
                    });
                    if (existingUser) {
                        throw Error("User with given e-mail already exists.");
                    }
                });
                // TODO .setOnce();

                return value;
            })(string({ validation: validation.create("required,email") }))
        })),
        withName("SecurityUser")
github webiny / webiny-js / packages / webiny-api-forms / src / models / Form.model.js View on Github external
withFields(instance => ({
            publishedOn: skipOnPopulate()(date()),
            name: string({ validation: validation.create("required") }),
            content: string(),
            version: number(),
            parent: string(),
            published: onSet(value => {
                // Deactivate previously published revision
                if (value && value !== instance.published && instance.isExisting()) {
                    instance.locked = true;
                    instance.publishedOn = new Date();
                    instance.registerHookCallback("beforeSave", async () => {
                        // Deactivate previously published revision
                        const publishedRev = (await getModel("Form").findOne({
                            query: { published: true, parent: instance.parent }
                        }): any);

                        if (publishedRev) {
                            publishedRev.published = false;
github webiny / webiny-js / packages-client / webiny-client-ui / src / components / Tags / index.js View on Github external
validateTag(value = null) {
        return validation.validate(value, this.props.validateTags);
    }
github webiny / webiny-js / packages / webiny-model / src / attribute.js View on Github external
async validateAttribute(value: mixed) {
        let validators = this.getValidators();
        if (typeof validators === "string") {
            try {
                await validation.validate(value, validators);
            } catch (e) {
                throw new ModelError("Invalid attribute.", ModelError.INVALID_ATTRIBUTE, {
                    message: e.message,
                    value: e.value,
                    validator: e.validator
                });
            }
        } else if (typeof validators === "function") {
            await validators(value, this);
        }
    }
github webiny / webiny-js / packages / webiny-client-ui / src / components / Tags / index.js View on Github external
validateTag(value = null) {
        if (typeof this.props.validate === "string") {
            return validation.validate(value, this.props.validateTags);
        }
    }
github webiny / webiny-js / packages-utils / webiny-model / src / attribute.js View on Github external
async validateAttribute(value: mixed) {
        let validators = this.getValidators();
        if (typeof validators === "string") {
            try {
                await validation.validate(value, validators);
            } catch (e) {
                throw new ModelError("Invalid attribute.", ModelError.INVALID_ATTRIBUTE, {
                    message: e.message,
                    value: e.value,
                    validator: e.validator
                });
            }
        } else if (typeof validators === "function") {
            await validators(value, this);
        }
    }
github webiny / webiny-js / packages / webiny-app-ui / src / components / Tags / index.js View on Github external
validateTag(value = null) {
        if (typeof this.props.validate === "string") {
            return validation.validate(value, this.props.validateTags);
        }
    }
github webiny / webiny-js / packages / webiny-api-security / src / models / SecurityRole.model.js View on Github external
const existingRole = await getModel("SecurityRole").findOne({
                    query: { slug: this.slug }
                });
                if (existingRole) {
                    throw Error(`Role with slug "${this.slug}" already exists.`);
                }
            },
            async beforeDelete() {
                if (this.system) {
                    throw Error(`Cannot delete system role.`);
                }
            }
        }),
        withFields({
            name: string({ validation: validation.create("required") }),
            slug: setOnce()(string({ validation: validation.create("required") })),
            description: string(),
            scopes: string({ list: true }),
            system: boolean()
        }),
        withName("SecurityRole")
    )(Model);
github webiny / webiny-js / packages / webiny-form / src / validation.js View on Github external
getValidator(name: string): Validator {
        const validator = validation.getValidator(name);
        if (!validator) {
            throw new ValidationError("Validator `" + name + "` does not exist!", name);
        }
        return validator;
    }

webiny-validation

A simple data validation library, packed with frequently used validators.

MIT
Latest version published 5 years ago

Package Health Score

66 / 100
Full package analysis

Similar packages