-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: promises where not allowed by type #61
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sometimes people don't use real promises. Its sad, but true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👌 |
||
return v instanceof Promise || (isObj(v) && typeof v.then === 'function'); | ||
} | ||
/** | ||
* Create a data validator | ||
* @param data | ||
|
@@ -19,6 +24,8 @@ export function createValidator<V, D, E>(data?: D, name?: string | string[]) { | |
name: Array.isArray(name) ? name.join('.') : name | ||
}) | ||
}; | ||
} else if (isPromiseLike(checkResult)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to be able to add test cases for the changes here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point added a test. |
||
throw new Error('synchronous validator had an async result'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like the safest thing to do. I would hate for somebody to make an async call that never resolves. |
||
} else if (typeof checkResult === 'object' && (checkResult.hasError || checkResult.array)) { | ||
return checkResult; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this didn't work before I made changes, kept returning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dtslint
is a linting tool used by the current library before refactoring through typescript, I think it can be removed, because there is eslint as a type checking tool.