From 55c92b0d41126e3ba6b53ff847c1197b8c0bf535 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Thu, 18 Apr 2024 13:26:54 +0800 Subject: [PATCH 1/2] chore(deps): use nanoid instead of cuid --- package.json | 2 +- src/types/cuid.ts | 10 +++++----- test/scripts/model.ts | 4 ++-- test/scripts/types/cuid.ts | 4 +++- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index bfc6723b..e7161df6 100644 --- a/package.json +++ b/package.json @@ -30,11 +30,11 @@ "license": "MIT", "dependencies": { "bluebird": "^3.7.2", - "cuid": "^2.1.8", "graceful-fs": "^4.2.10", "hexo-log": "^4.0.1", "is-plain-object": "^5.0.0", "jsonparse": "^1.3.1", + "nanoid": "^3.3.7", "rfdc": "^1.3.0", "through2": "^4.0.2" }, diff --git a/src/types/cuid.ts b/src/types/cuid.ts index 476cfa0c..bd2a715d 100644 --- a/src/types/cuid.ts +++ b/src/types/cuid.ts @@ -1,9 +1,9 @@ import SchemaType from '../schematype'; -import cuid from 'cuid'; +import { nanoid } from 'nanoid'; import ValidationError from '../error/validation'; /** - * [CUID](https://github.com/ericelliott/cuid) schema type. + * [CUID](https://github.com/ai/nanoid) schema type. */ class SchemaTypeCUID extends SchemaType { @@ -16,20 +16,20 @@ class SchemaTypeCUID extends SchemaType { */ cast(value?: string): string { if (value == null && this.options.required) { - return cuid(); + return nanoid(); } return value; } /** - * Validates data. A valid CUID must be started with `c` and 25 in length. + * Validates data. A valid CUID must be 21 in length. * * @param {*} value * @return {String|Error} */ validate(value?: string): string { - if (value && (value[0] !== 'c' || value.length !== 25)) { + if (value && (value.length !== 21)) { throw new ValidationError(`\`${value}\` is not a valid CUID`); } diff --git a/test/scripts/model.ts b/test/scripts/model.ts index c5b20116..84dd460c 100644 --- a/test/scripts/model.ts +++ b/test/scripts/model.ts @@ -7,7 +7,7 @@ import lodash from 'lodash'; const { sortBy } = lodash; import Promise from 'bluebird'; import sinon from 'sinon'; -import cuid from 'cuid'; +import { nanoid } from 'nanoid'; import Database from '../../dist/database'; import type Query from '../../dist/query'; import type Document from '../../dist/document'; @@ -214,7 +214,7 @@ describe('Model', () => { }).then(data => User.removeById(data._id))); it('save() - sync problem', () => { - const id = cuid(); + const id = 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 9023eb8a..c52ee7bd 100644 --- a/test/scripts/types/cuid.ts +++ b/test/scripts/types/cuid.ts @@ -1,5 +1,6 @@ import chai from 'chai'; const should = chai.should(); // eslint-disable-line +import { nanoid } from 'nanoid'; import ValidationError from '../../../dist/error/validation'; import SchemaTypeCUID from '../../../dist/types/cuid'; @@ -17,7 +18,8 @@ describe('SchemaTypeCUID', () => { }); it('validate()', () => { - type.validate('ch72gsb320000udocl363eofy').should.eql('ch72gsb320000udocl363eofy'); + const id = nanoid(); + type.validate(id).should.eql(id); (() => type.validate('foo')).should.to.throw(ValidationError, '`foo` is not a valid CUID'); }); 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 2/2] 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 bd2a715d..c361d047 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 84dd460c..13d6e767 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 c52ee7bd..86a4aa9e 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');