diff --git a/ckanext/scheming/ckan_formpages.yaml b/ckanext/scheming/ckan_formpages.yaml index 4ec88909..c2ace29c 100644 --- a/ckanext/scheming/ckan_formpages.yaml +++ b/ckanext/scheming/ckan_formpages.yaml @@ -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: @@ -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 @@ -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 @@ -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 diff --git a/ckanext/scheming/validation.py b/ckanext/scheming/validation.py index d91dd13d..e8cb006b 100644 --- a/ckanext/scheming/validation.py +++ b/ckanext/scheming/validation.py @@ -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 = {} @@ -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']]) @@ -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