Skip to content

Commit

Permalink
dataset completeness example
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Jul 1, 2024
1 parent 27035f4 commit d43207d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions ckanext/scheming/ckan_formpages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dataset_type: formpages
about: The default CKAN dataset schema with form split across multiple pages
about_url: http://github.com/ckan/ckanext-scheming

draft_fields_required: false

dataset_fields:

Expand Down Expand Up @@ -54,6 +55,8 @@ dataset_fields:
label: Version
validators: ignore_missing unicode_safe package_version_validator
form_placeholder: '1.0'
required: true
validators: scheming_required unicode_safe package_version_validator

- start_form_page:
title: Contact Info
Expand All @@ -63,6 +66,8 @@ dataset_fields:
label: Author
form_placeholder: Joe Bloggs
display_property: dc:creator
required: true
validators: scheming_required unicode_safe

- field_name: author_email
label: Author Email
Expand Down Expand Up @@ -95,6 +100,8 @@ resource_fields:
- field_name: name
label: Name
form_placeholder: eg. January 2011 Gold Prices
required: true
validators: scheming_required unicode_safe

- field_name: description
label: Description
Expand Down
29 changes: 21 additions & 8 deletions ckanext/scheming/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
import ckanext.scheming.helpers as sh
from ckanext.scheming.errors import SchemingException

OneOf = get_validator('OneOf')
ignore_missing = get_validator('ignore_missing')
not_empty = get_validator('not_empty')

all_validators = {}


Expand Down Expand Up @@ -65,6 +61,7 @@ def scheming_choices(field, schema):
"""
Require that one of the field choices values is passed.
"""
OneOf = get_validator('OneOf')
if 'choices' in field:
return OneOf([c['value'] for c in field['choices']])

Expand All @@ -84,11 +81,27 @@ def validator(value):
@register_validator
def scheming_required(field, schema):
"""
not_empty if field['required'] else ignore_missing
return a validator based on field['required']
and schema['draft_fields_required'] setting
"""
if field.get('required'):
return not_empty
return ignore_missing
if not field.get('required'):
return get_validator('ignore_missing')
if not schema.get('draft_fields_required', True):
return get_validator('scheming_draft_fields_not_required')
return get_validator('not_empty')


@register_validator
def scheming_draft_fields_not_required(key, data, errors, context):
"""
call ignore_missing if state is draft, otherwise not_empty
"""
state = data.get(('state',), '')
if state.startswith('draft'):
v = get_validator('ignore_missing')
else:
v = get_validator('not_empty')
v(key, data, errors, context)


@scheming_validator
Expand Down

0 comments on commit d43207d

Please sign in to comment.