elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.48k stars 200 forks source link

Schema validation incorrect on nested fields? #482

Open m1212e opened 6 months ago

m1212e commented 6 months ago

What version of Elysia.JS is running?

0.8.17

What platform is your computer?

Linux 6.1.0-18-amd64 x86_64 unknown

What steps can reproduce the bug?

Clone https://github.com/m1212e/elysia-schema-bug and see src/index.ts for the details

What is the expected behavior?

The schema should be validated correctly

What do you see instead?

Invalid fields are just passed along

Additional information

No response

mtt-artis commented 6 months ago

seems like Pick and Composite don t check additional properties

import { TypeCompiler } from '@sinclair/typebox/compiler'
TypeCompiler.Code(
    t.Composite([
        t.Pick(Resource, ["name"]),
        t.Object({
            associatedScopes: t.Optional(t.Array(t.Pick(Scope, ["id"]))),
        })
    ])
)
return function check(value) {
  return (
    (typeof value === 'object' && value !== null && !Array.isArray(value)) &&
    (typeof value.name === 'string') &&
    (value.associatedScopes !== undefined ? (Array.isArray(value.associatedScopes) && value.associatedScopes.every((value) => ((typeof value === 'object' && value !== null && !Array.isArray(value)) && (typeof value.id === 'string')))) : true)
  )
m1212e commented 6 months ago

Typebox Objects have the additionalFields option which specifies this. I'll check if thats what I need.