Giter VIP home page Giter VIP logo

Comments (14)

LucasLundJensen avatar LucasLundJensen commented on August 26, 2024 3

Is this broken again?
I'm getting this error when attempting to have an optional field in the body.

Using 0.7.30

app
    .post("/getUser", ({ body }) => getUser(body.email), {
    body: t.Object({ email: t.Optional(t.String()) })
})
{
    "code": "VALIDATION",
    "status": 400,
    "type": "body",
    "validator": {
        "schema": {
            "type": "object",
            "properties": {
                "email": {
                    "type": "string"
                }
            },
            "additionalProperties": false
        },
        "references": [],
        "code": "return function check(value) {\n  return (\n    (typeof value === 'object' && value !== null && !Array.isArray(value)) &&\n    (value.email !== undefined ? ((typeof value.email === 'string')) : true) &&\n    Object.getOwnPropertyNames(value).every(key => ['email'].includes(key))\n  )\n}",
        "hasTransform": false
    }
}

from elysia.

akainocode avatar akainocode commented on August 26, 2024 1

Confirming that this isn't working with t.Optional(t.Boolean()) as well.

from elysia.

SaltyAom avatar SaltyAom commented on August 26, 2024

Fixes with 9192fa9, update to Elysia 0.5.23 for the fix.

Please let me know if this fix your error.

from elysia.

mckamyk avatar mckamyk commented on August 26, 2024

EDIT: Both optional and non-optional Numeric doesn't seem to work with 0.5.23. Both appear to work in the following snippet in 0.5.22

0.5.23 seems to have broken non-optional validation for me.

       .post('/todos/toggle/:id', ({params}) => {
		const todo = db.find(todo => todo.id === params.id)
		if (todo) {
			todo.completed = !todo.completed
			return <TodoItem {...todo} />
		}
	}, {
		params: t.Object({
			id: t.Numeric() // or t.Optional(t.Numeric()), same behavior
		})
	})

Works fine with 0.5.22, but when using 0.5.23 I get the Invalid params: 'id'. Expected number

from elysia.

vrn-dev avatar vrn-dev commented on August 26, 2024

I can confirm that optional query object are working as intended but has indeed broken t.Numeric() coercion in params object.

.get('/test/:id/:id2/:id3', ({query}) => {
		return {
			query
		}
	}, {
		query: t.Object({
			pageNum: t.Optional(t.Numeric({ default: 1 })),  // <----- Works
			pageSize: t.Optional(t.Numeric({ default: 10 })), // <----- Works
		}),
		params: t.Object({
			id: t.Numeric(),             // <------ Invalid params: 'id'. Expected number
                        id2: t.Optional(t.Numeric),  // <------ Invalid params: 'id'. Expected number
                        id3: t.String()              // <------ Works
		})
	})

from elysia.

SaltyAom avatar SaltyAom commented on August 26, 2024

Right, numeric path parameters should be fixed by 0.5.24.

I accidentally used query check on params which caused this error that was used to fix 0.5.23, you can see the commit here 4d8c6ca

from elysia.

pacmanmati avatar pacmanmati commented on August 26, 2024

t.Numeric() coercion works for me after upgrading to 0.5.25 (latest)

from elysia.

SaltyAom avatar SaltyAom commented on August 26, 2024

Closing this issue as the use of t.Numeric and no update.

Feels free to reopen the issue if the problem still insists.

from elysia.

Ernxst avatar Ernxst commented on August 26, 2024

This seems to be broken again (using Bun v0.7.3 but also noticed this issue with pnpm 8.6.10).

Making a post request to following route does not work on 0.6.9 when aot is disabled:

import { Elysia, t } from "elysia";

new Elysia({ aot: false })
  .post("/sign-in", ({ query }) => {}, {
    query: t.Object({
      redirect_uri: t.Optional(t.String()),
    }),
  })
  .listen(8080);

Gives the following error:

Invalid query, 'http://localhost:8080/sign-in': Unexpected property

Expected: {}

Found: {
  "http://localhost:8080/sign-in": ""
}

Enabling aot seems to fix this.

from elysia.

SaltyAom avatar SaltyAom commented on August 26, 2024

Should be fixed in 0.6.10

from elysia.

spanwair avatar spanwair commented on August 26, 2024

same issue .. Elysia please repair this bug

from elysia.

spanwair avatar spanwair commented on August 26, 2024

I found a work around for using optional object type:

t.Optional(t.Partial(t.Object({ noError: t.String() })))

from elysia.

QAdamek avatar QAdamek commented on August 26, 2024

i have the same issue :(

from elysia.

JairZijp avatar JairZijp commented on August 26, 2024

Same here with t.Optional(t.String())

from elysia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.