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

Keys() needs to set skip to false after iteration. #5

Closed
grisha opened this issue Mar 5, 2024 · 2 comments · Fixed by #4
Closed

Keys() needs to set skip to false after iteration. #5

grisha opened this issue Mar 5, 2024 · 2 comments · Fixed by #4

Comments

@grisha
Copy link
Contributor

grisha commented Mar 5, 2024

Using the README example with a small change (add optional "foo") will result in the Without() being ignored. This is because AnySchema.Optional() calls ctx.Skip():

package main

import (
    "fmt"
    "log"

    "github.com/faceair/jio"
)

func jioNotAllowed(ctx *jio.Context) {
    ctx.Abort(fmt.Errorf("Attribute `%s` not allowed in this context.", ctx.FieldPath()))
    return
}

func main() {
    data := []byte(`{                                                                                                                                                                                                                                     
        "debug": "on",                                                                                                                                                                                                                                    
        "window": {                                                                                                                                                                                                                                       
            "title": "Sample Widget",                                                                                                                                                                                                                     
            "size": [500, 500]                                                                                                                                                                                                                            
        }                                                                                                                                                                                                                                                 
    }`)
    _, err := jio.ValidateJSON(&data, jio.Object().Keys(jio.K{
        "debug": jio.Bool().Truthy("on").Required(),
        "window": jio.Object().Keys(jio.K{
            "title": jio.String().Min(3).Max(18),
            "size":  jio.Array().Items(jio.Number().Integer()).Length(2).Required(),
            "foo":   jio.Any(),
        }).Without("name", "title").Required(),
    }))
    if err != nil {
        panic(err)
    }
    log.Printf("%s", data)
}

This results in no errors, even though we should expect "panic: field window contains title"

If you remove the "foo" line, it works correctly:

panic: field `window` contains title

The problem is in the ObjectSchema.Keys() - it needs to set ctx.skip to false after the for loop.

@faceair
Copy link
Owner

faceair commented Jul 3, 2024

Sorry for being late, I really haven't paid attention to this reminder for a long time.

Thanks for pointing out the issue and giving a fix PR.

@faceair
Copy link
Owner

faceair commented Jul 4, 2024

I added the tag for v1.0.2.

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 a pull request may close this issue.

2 participants