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

Issue on Asynchronous Validators with Deno #76

Closed
KirianCaumes opened this issue Nov 21, 2020 · 4 comments
Closed

Issue on Asynchronous Validators with Deno #76

KirianCaumes opened this issue Nov 21, 2020 · 4 comments
Labels
awaiting feedback More data is needed before taking any more actions

Comments

@KirianCaumes
Copy link

Hi,

I've found an issue with asynchronous validators.

For example, correct behavior like this:

import Schema, { string } from 'https://denoporter.sirjosh.workers.dev/v1/deno.land/x/computed_types/src/index.ts'
const UserSchema = {
    username: string.trim().normalize().between(7, 255),
};
const validator = Schema(UserSchema);
try {
    const user = await validator({ username: 'test' });
    console.log(user)
} catch (error) {
    console.log(error)
    console.log(error.errors[0])
    console.log(error.errors[0]?.error?.message)
    console.log(error.errors[0]?.path)
}

Print this in console, wich is ok:

ValidationError: username: Expect length to be between 7 and 255 characters (actual: 4)
    at toError (errors.ts:33:16)
    at createValidationError (errors.ts:51:34)
    at compiler.ts:102:23
    at test.ts:14:24
{
  error: RangeError: Expect length to be between 7 and 255 characters (actual: 4)
    at string.ts:36:35
    at toError (errors.ts:36:24)
    at validations.ts:25:19
    at Validator.ts:41:24
    at Array.<anonymous> (compiler.ts:58:40)
    at compiler.ts:96:37
    at test.ts:14:24,
  path: [ "username" ]
}
Expect length to be between 7 and 255 characters (actual: 4)
[ "username" ]

But with async like this:

import Schema, { string } from 'https://denoporter.sirjosh.workers.dev/v1/deno.land/x/computed_types/src/index.ts'
async function AvailableUsername(input: string) {
    throw new TypeError('my error')
}
const UserSchema = {
    username: AvailableUsername,
};
const validator = Schema(UserSchema);
try {
    const user = await validator({ username: 'test' });
    console.log(user)
} catch (error) {
    console.log(error)
    console.log(error.errors[0])
    console.log(error.errors[0]?.error?.message)
    console.log(error.errors[0]?.path)
}

Return this:

ValidationError: undefined
    at toError (errors.ts:33:16)
    at createValidationError (errors.ts:51:34)
    at compiler.ts:108:23
    at async test.ts:14:18
TypeError: my error
    at AvailableUsername (test.ts:4:11)
    at Array.<anonymous> (compiler.ts:58:40)
    at compiler.ts:96:37
    at test.ts:14:24
undefined
undefined

So asynchronous validators does not seems to work well with Deno :/

Any idea?

Thanks a lot!

moshest added a commit that referenced this issue Nov 24, 2020
@moshest
Copy link
Member

moshest commented Nov 24, 2020

Hi, thank you for the report.

I check this bug but I didn't manage to reproduce it on Node.js (See: 548ff93).

I'm thinking it may related to some versions update and fixes that deployed so I just released a new version v1.3.1
Can you try reproduce this error on Node.js environment as well?

@moshest moshest added the awaiting feedback More data is needed before taking any more actions label Nov 24, 2020
@KirianCaumes
Copy link
Author

Hi,

Thanks you answer!

I just tested again it with Deno and Node:

  • It works with Node, but if I remember well, it already worked before.
  • However it still doesn't seem to work with Deno.

I've tried to refresh your lib with this command, but I'm not sure if I'm doing well as I'm a very beginner with Deno.

deno cache --reload https://denoporter.sirjosh.workers.dev/v1/deno.land/x/computed_types/src/index.ts

However, if i throw an error like this in Deno, it works, but that's not an awesome solution:

throw {
    path: ["key"],
    error: new Error("my error")
}

Thanks for you help!

@moshest
Copy link
Member

moshest commented Nov 26, 2020

Thanks for verifying. I dig in and found deno.land added a webhook to install on repos. I didn't do that up until now so it didn't push any new updates to deno 🙀

I push a new verison and I verified it got updated on deno. Please kindly check again now :)

@KirianCaumes
Copy link
Author

Hi,
Thank you very much for this, it works now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting feedback More data is needed before taking any more actions
Projects
None yet
Development

No branches or pull requests

2 participants