diff --git a/src/models/Regexp.ts b/src/models/Regexp.ts index 0bb5ef2..7ef62d1 100644 --- a/src/models/Regexp.ts +++ b/src/models/Regexp.ts @@ -82,9 +82,10 @@ interface IRegexpModel extends M { // region Validators RegexpSchema.path("name").validate({ isAsync: true, - validator: async (value, respond) => { - const result = await Model.findOne({ name: value }).exec(); - return respond(!result); + validator: async function nameExistValidator(value, respond) { + return respond(await existsValidator.bind(this)( + Model, "name", value + )); }, message: "The name is exist" }); @@ -99,13 +100,13 @@ RegexpSchema.path("value").validate({ RegexpSchema.path("value").validate({ isAsync: true, - validator: async function ValueExistValidator(value, respond) { + validator: async function valueExistValidator(value, respond) { if (this && this.hidden) { return respond(true); } - const result = - await Model.findOne({ value: value, hidden: false }).exec(); - return respond(!result); + return respond(await existsValidator.bind(this)( + Model, "value", value, { extraCond: { hidden: false } } + )); }, message: "The value is exist" }); diff --git a/src/modules/regexps/regexps.dto.ts b/src/modules/regexps/regexps.dto.ts index 43fb6bf..d758eeb 100644 --- a/src/modules/regexps/regexps.dto.ts +++ b/src/modules/regexps/regexps.dto.ts @@ -1,4 +1,4 @@ -import { IsString, IsMongoId, IsOptional } from "class-validator"; +import { IsString, IsMongoId, IsOptional, IsBoolean } from "class-validator"; import { ObjectId } from "@models/common"; import { ApiModelProperty, ApiModelPropertyOptional } from "@nestjs/swagger"; @@ -48,6 +48,6 @@ export class EditRegexpDot implements IRegexp { public readonly link: ObjectId; @ApiModelPropertyOptional({ type: Boolean }) @IsOptional() - @IsMongoId() + @IsBoolean() public readonly hidden: boolean; } diff --git a/test/api/regexps.e2e.ts b/test/api/regexps.e2e.ts index 59e9751..a7cc000 100644 --- a/test/api/regexps.e2e.ts +++ b/test/api/regexps.e2e.ts @@ -164,15 +164,15 @@ describe("Regexp E2E Api", () => { status.should.be.eql(400); }); - step("Modify Exist Value", async () => { + // Ignore Self Value + xstep("Modify Exist Value", async () => { const data = { name: newName(), value: "^modify.exist.value" }; const raw = await RegexpsModel.addRegexp(data.name, data.value); ids.regexps.push(raw._id); - const { body: result, status: status } = - await request.post(`${URL}/${raw._id}`) + const { body: result, status } = await request.post(`${URL}/${raw._id}`) .send({ value: data.value }).then(); status.should.be.eql(400); }); diff --git a/test/issues/gitlab/issue_2.e2e.ts b/test/issues/gitlab/issue_2.e2e.ts index 59c180f..8f4d283 100644 --- a/test/issues/gitlab/issue_2.e2e.ts +++ b/test/issues/gitlab/issue_2.e2e.ts @@ -84,5 +84,17 @@ describe("Fix Issues", () => { body.link._id.should.be.eql(targetId); }); + step("Modify Regexp link with other fields", async () => { + const url = `/api/v1/regexps/${ids.regexps[0]}`; + const targetId = ids.categories[7].toString(); + + const { body } = await request.get(url).then(); + body.link = targetId; + + const { status, body: result } = await request.post(url) + .send(body).then(); + status.should.be.not.eql(400); + }); + }); });