Skip to content

Commit

Permalink
fix: promises where not allowed by type (#61)
Browse files Browse the repository at this point in the history
* fix: promises where not allowed by type

* fix: added test

* removed test:types as it is no longer needed nor working

* improved error message
  • Loading branch information
jspears authored Mar 1, 2023
1 parent b202fe0 commit 9cc665c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
"build:types": "npx tsc --emitDeclarationOnly --outDir lib && npx tsc --emitDeclarationOnly --outDir es",
"prepublishOnly": "npm run test && npm run build",
"tdd": "mocha --watch",
"test": "npm run lint && npm run test:once && npm run test:types",
"test": "npm run lint && npm run test:once",
"test:once": "nyc --reporter=lcovonly --reporter=html mocha",
"test:types": "dtslint --expectOnly --localTs node_modules/typescript/lib types",
"doctoc:": "doctoc README.md",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
},
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type ValidCallbackType<V, D, E> = (
value: V,
data?: D,
filedName?: string | string[]
) => CheckResult<E> | boolean;
) => CheckResult<E> | boolean | Promise<boolean | CheckResult<E>>;
export type PlainObject<T extends Record<string, unknown> = any> = {
[P in keyof T]: T;
};
Expand Down
11 changes: 10 additions & 1 deletion src/utils/createValidator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { CheckResult, RuleType } from '../types';
import formatErrorMessage from './formatErrorMessage';

function isObj(o: unknown): o is Record<PropertyKey, unknown> {
return o != null && (typeof o === 'object' || typeof o == 'function');
}
function isPromiseLike(v: unknown): v is Promise<unknown> {
return v instanceof Promise || (isObj(v) && typeof v.then === 'function');
}
/**
* Create a data validator
* @param data
Expand All @@ -19,6 +24,10 @@ export function createValidator<V, D, E>(data?: D, name?: string | string[]) {
name: Array.isArray(name) ? name.join('.') : name
})
};
} else if (isPromiseLike(checkResult)) {
throw new Error(
'synchronous validator had an async result, you should probably call "checkAsync()"'
);
} else if (typeof checkResult === 'object' && (checkResult.hasError || checkResult.array)) {
return checkResult;
}
Expand Down
19 changes: 17 additions & 2 deletions test/MixedTypeSpec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
require('chai').should();

const chai = require('chai');
const schema = require('../src');
chai.should();
const { StringType, SchemaModel, NumberType, ArrayType, MixedType } = schema;

describe('#MixedType', () => {
Expand Down Expand Up @@ -401,4 +401,19 @@ describe('#MixedType', () => {
checkResult.contact.hasError.should.equal(true);
checkResult.contact.errorMessage.should.equal('error2');
});

it('should error when an async rule is executed by the sync validator', () => {
const m = MixedType().addRule(async () => {
return true;
}, 'An async error');
let err;
try {
m.check({});
} catch (e) {
err = e;
}
chai
.expect(err?.message)
.to.eql('synchronous validator had an async result, you should probably call "checkAsync()"');
});
});

0 comments on commit 9cc665c

Please sign in to comment.