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

map( key=inlclude() ) validate all keys #264

Open
sambowry opened this issue Mar 2, 2025 · 1 comment
Open

map( key=inlclude() ) validate all keys #264

sambowry opened this issue Mar 2, 2025 · 1 comment
Assignees
Labels

Comments

@sambowry
Copy link

sambowry commented Mar 2, 2025

I tried to use key=include() in map() to add meaningful names to the schema, but it looks that the include() is not evaluated and it validates all keys.

test.yml

data:
  a: 1
  b: 2
  c: 3

schema.yml

# not correct, all keys are valid
data: map( int(), key=include('letter_a') )

# correct, only 'a' is valid
# data: map( int(), key=str( matches='^[a]$' ) )

---
letter_a: str( matches='^[a]$' )
@sambowry
Copy link
Author

sambowry commented Mar 3, 2025

Now it works for me.

from .. import yamale
. . .
class Schema(object):
  . . .
    def __init__(self, schema_dict, name="", validators=None, includes=None):
        . . .
        yamale.schema = self

class Include(Validator):
  . . .
    def _is_valid(self, value):
        errors = []
        if isinstance(value,str):
            errors += yamale.schema.includes[self.include_name].validate( value, self.include_name, self.strict ).errors
        return not errors

### test schema:
data: include('data')
---
letter_a: str( matches='^[a]$' )
letter_b: str( matches='^[b]$' )
class_name: any( include('letter_a'), include('letter_b') )
data:
  first:  map( int(), key=include('class_name') )
  second: map( int(), key=include('letter_b'  ) )

### test data:
data:
  first:
    a: 1
    b: 2
    c: 3
  second:
    a: 1
    b: 2
    c: 3

### result:
Validating test.yml...
Validation failed!
Error validating data 'test.yml' with schema 'test-schema.yml'
        data.first: Key error - 'c' is not a class_name.
        data.second: Key error - 'a' is not a letter_b.
        data.second: Key error - 'c' is not a letter_b.

@cblakkan cblakkan self-assigned this Mar 3, 2025
@cblakkan cblakkan added the bug label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants