From 1dde7cb608e7892a7a174ca5f1b611211438caab Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Thu, 18 Apr 2024 14:38:25 +0800 Subject: [PATCH] fix compatibility issue --- src/types/cuid.ts | 6 +++--- test/scripts/model.ts | 2 +- test/scripts/types/cuid.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/types/cuid.ts b/src/types/cuid.ts index bd2a715..c361d04 100644 --- a/src/types/cuid.ts +++ b/src/types/cuid.ts @@ -16,20 +16,20 @@ class SchemaTypeCUID extends SchemaType { */ cast(value?: string): string { if (value == null && this.options.required) { - return nanoid(); + return 'cuid' + nanoid(); } return value; } /** - * Validates data. A valid CUID must be 21 in length. + * Validates data. A valid CUID must be started with `c` and 25 in length. * * @param {*} value * @return {String|Error} */ validate(value?: string): string { - if (value && (value.length !== 21)) { + if (value && (value[0] !== 'c' || value.length !== 25)) { throw new ValidationError(`\`${value}\` is not a valid CUID`); } diff --git a/test/scripts/model.ts b/test/scripts/model.ts index 84dd460..13d6e76 100644 --- a/test/scripts/model.ts +++ b/test/scripts/model.ts @@ -214,7 +214,7 @@ describe('Model', () => { }).then(data => User.removeById(data._id))); it('save() - sync problem', () => { - const id = nanoid(); + const id = 'cuid' + nanoid(); return Promise.all([ User.save({_id: id, age: 1}), diff --git a/test/scripts/types/cuid.ts b/test/scripts/types/cuid.ts index c52ee7b..86a4aa9 100644 --- a/test/scripts/types/cuid.ts +++ b/test/scripts/types/cuid.ts @@ -18,7 +18,7 @@ describe('SchemaTypeCUID', () => { }); it('validate()', () => { - const id = nanoid(); + const id = 'cuid' + nanoid(); type.validate(id).should.eql(id); (() => type.validate('foo')).should.to.throw(ValidationError, '`foo` is not a valid CUID');