Skip to content

Commit

Permalink
fix compatibility issue
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Apr 18, 2024
1 parent 55c92b0 commit 1dde7cb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/types/cuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ class SchemaTypeCUID extends SchemaType<string> {
*/
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`);
}

Expand Down
2 changes: 1 addition & 1 deletion test/scripts/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}),
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/types/cuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 1dde7cb

Please sign in to comment.