Skip to content
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

Merged
merged 4 commits into from
Mar 1, 2023

Conversation

jspears
Copy link
Contributor

@jspears jspears commented Mar 1, 2023

This adds typescript types allowing for promise based validators.

#60

@@ -13,7 +13,7 @@
"tdd": "mocha --watch",
"test": "npm run lint && npm run test:once && npm run test:types",
"test:once": "nyc --reporter=lcovonly --reporter=html mocha",
"test:types": "dtslint --expectOnly --localTs node_modules/typescript/lib types",
Copy link
Contributor Author

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

> dtslint --expectOnly --localTs node_modules/typescript/lib types

Error: Errors in typescript@local for external dependencies:
../src/Schema.ts(33,37): error TS2345: Argument of type 'DataType' is not assignable to parameter of type 'PlainObject<any>'.
../src/Schema.ts(48,37): error TS2345: Argument of type 'DataType' is not assignable to parameter of type 'PlainObject<any>'.

    at /Users/jspears/dev/schema-typed/node_modules/dtslint/bin/index.js:207:19
    at Generator.next (<anonymous>)
    at fulfilled (/Users/jspears/dev/schema-typed/node_modules/dtslint/bin/index.js:6:58)

Copy link
Member

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.

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> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes people don't use real promises. Its sad, but true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@@ -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)) {
throw new Error('synchronous validator had an async result');
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point added a test.

@simonguo simonguo merged commit 9cc665c into rsuite:master Mar 1, 2023
@simonguo
Copy link
Member

simonguo commented Mar 1, 2023

Fixed in 2.0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants