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

JSON schema does not allow strings on top-level #10542

Open
cbratschi opened this issue Jan 13, 2025 · 1 comment
Open

JSON schema does not allow strings on top-level #10542

cbratschi opened this issue Jan 13, 2025 · 1 comment
Labels
status: needs-triage Possible bug which hasn't been reproduced yet

Comments

@cbratschi
Copy link

Describe the Bug

The following code always parses strings as JSON without checking the schema:

JSON.parse(siblingData[field.name] as string)

If the following schema is being used, the JSON.parse() call fails but the value is valid:

                                        jsonSchema: {
                                            uri: 'a://b/foo.json', //dummy
                                            fileMatch: [ 'a://b/foo.json' ], //dummy

                                            schema: {
                                                        anyOf: [{
                                                            type: 'string'
                                                        }, {
                                                            type: 'number'
                                                        }, {
                                                            type: 'boolean'
                                                        }]
                                                    }
                                            }

Best solution is to check if the string value passes the schema before checking jsonError:

return t('validation:invalidInput')

Link to the code that reproduces this issue

https://github.com/cbratschi/payload

Reproduction Steps

Binaries:
Node: 22.12.0
npm: 10.9.0
Yarn: 1.22.22
pnpm: N/A
Relevant Packages:
payload: 3.13.0
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:05:23 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6031
Available memory (MB): 65536
Available CPU cores: 16

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

See above.
@cbratschi cbratschi added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Jan 13, 2025
@cbratschi
Copy link
Author

Workaround was:

                                    //data (JSON)
                                    <JSONField>{
                                        name: 'data',
                                        type: 'json',

                                        jsonSchema: {
                                            uri: 'a://b/foo.json', //dummy
                                            fileMatch: [ 'a://b/foo.json' ], //dummy

                                            schema: {
                                                //see https://ajv.js.org/strict-mode.html#strict-types
                                                //Note: cannot use a string here, have to use an object (see https://github.com/payloadcms/payload/blob/afcc970e3609c1bb6f2d59c4cbbb36ac62730ec9/packages/payload/src/fields/hooks/beforeChange/promise.ts#L137)
                                                //see https://github.com/payloadcms/payload/issues/10542

                                                //Note: we better use an object here, can still add more values in future
                                                type: 'object',

                                                properties: {
                                                    value: {
                                                        anyOf: [{
                                                            type: 'string'
                                                        }, {
                                                            type: 'number'
                                                        }, {
                                                            type: 'boolean'
                                                        }]
                                                    }
                                                },

                                                additionalProperties: false
                                            }
                                        },
                                    required: false
                                },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs-triage Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

1 participant