diff --git a/package.json b/package.json index e45487da..9e15ce4f 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,8 @@ "author": "Tommy Chen (https://zespia.tw)", "license": "MIT", "dependencies": { + "@paralleldrive/cuid2": "^2.0.1", "bluebird": "^3.7.2", - "cuid": "^2.1.8", "graceful-fs": "^4.2.10", "hexo-log": "^4.0.1", "is-plain-object": "^5.0.0", diff --git a/src/types/cuid.ts b/src/types/cuid.ts index 49b057c6..59f77f4a 100644 --- a/src/types/cuid.ts +++ b/src/types/cuid.ts @@ -1,5 +1,5 @@ import SchemaType from '../schematype'; -import cuid from 'cuid'; +import { createId, getConstants } from '@paralleldrive/cuid2'; import ValidationError from '../error/validation'; /** @@ -16,20 +16,20 @@ class SchemaTypeCUID extends SchemaType { */ cast(value?) { if (value == null && this.options.required) { - return cuid(); + return createId(); } return value; } /** - * Validates data. A valid CUID must be started with `c` and 25 in length. + * Validates data. A valid CUID must be 24 in length. * * @param {*} value * @return {String|Error} */ validate(value?) { - if (value && (value[0] !== 'c' || value.length !== 25)) { + if (value && (value.length !== getConstants().defaultLength)) { throw new ValidationError(`\`${value}\` is not a valid CUID`); } diff --git a/test/scripts/model.ts b/test/scripts/model.ts index 87cfead4..94046c59 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 { createId } from '@paralleldrive/cuid2'; import Database from '../../dist/database'; describe('Model', () => { @@ -193,7 +193,7 @@ describe('Model', () => { }).then(data => User.removeById(data._id))); it('save() - sync problem', () => { - const id = cuid(); + const id = createId(); 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..f3ca1292 100644 --- a/test/scripts/types/cuid.ts +++ b/test/scripts/types/cuid.ts @@ -17,7 +17,7 @@ describe('SchemaTypeCUID', () => { }); it('validate()', () => { - type.validate('ch72gsb320000udocl363eofy').should.eql('ch72gsb320000udocl363eofy'); + type.validate('ch72gsb320000udocl363eof').should.eql('ch72gsb320000udocl363eof'); (() => type.validate('foo')).should.to.throw(ValidationError, '`foo` is not a valid CUID'); });