From 4b14b09adee2783a0d9dd6464faaee8ccc8717fc Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Sun, 20 Oct 2024 18:34:03 +0300 Subject: [PATCH] feat: add schema drop_unknown_fields --- ckanext/transmute/logic/action/get.py | 10 ++++++++++ setup.py | 15 +-------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/ckanext/transmute/logic/action/get.py b/ckanext/transmute/logic/action/get.py index 400e45d..f077810 100644 --- a/ckanext/transmute/logic/action/get.py +++ b/ckanext/transmute/logic/action/get.py @@ -79,7 +79,11 @@ def mutate_fields(data: dict[str, Any], definition: SchemaParser, root: str): """ schema = definition.types[root] + known_fields: set[str] = set() + for field_name, field in sorted(schema["fields"].items(), key=_weighten_fields): + known_fields.add(field_name) + if field.remove: data.pop(field_name, None) continue @@ -126,8 +130,14 @@ def mutate_fields(data: dict[str, Any], definition: SchemaParser, root: str): ) if field.map: + known_fields.add(field.map) data[field.map] = data.pop(field.name, None) + if schema.get("drop_unknown_fields"): + for name in list(data): + if name not in known_fields: + del data[name] + def _default_from(data, field: SchemaField): default_from: Union[list[str], str] = field.get_default_from() diff --git a/setup.py b/setup.py index 376813c..6068493 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,3 @@ -# -*- coding: utf-8 -*- from setuptools import setup -setup( - # If you are changing from the default layout of your extension, you may - # have to change the message extractors, you can read more about babel - # message extraction at - # http://babel.pocoo.org/docs/messages/#extraction-method-mapping-and-configuration - message_extractors={ - "ckanext": [ - ("**.py", "python", None), - ("**.js", "javascript", None), - ("**/templates/**.html", "ckan", None), - ], - } -) +setup()