Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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")
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;
validateTag(value = null) {
return validation.validate(value, this.props.validateTags);
}
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);
}
}
validateTag(value = null) {
if (typeof this.props.validate === "string") {
return validation.validate(value, this.props.validateTags);
}
}
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);
}
}
validateTag(value = null) {
if (typeof this.props.validate === "string") {
return validation.validate(value, this.props.validateTags);
}
}
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);
getValidator(name: string): Validator {
const validator = validation.getValidator(name);
if (!validator) {
throw new ValidationError("Validator `" + name + "` does not exist!", name);
}
return validator;
}