From a5d4fd41a4a1f681b117bf6f40ce1263382680a9 Mon Sep 17 00:00:00 2001 From: Damien Duhamel Date: Tue, 24 Apr 2018 18:28:09 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20try=20to=20validate=20an=20opti?= =?UTF-8?q?onal=20field=20if=20it=20is=20empty=20(#1237)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .all-contributorsrc | 4 ++- README.md | 2 +- .../Editor/EditorControlPane/Widget.js | 29 ++++++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 44c7930b..529b1271 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -176,7 +176,9 @@ "name": "Damien Duhamel", "avatar_url": "https://avatars3.githubusercontent.com/u/3051737?v=4", "profile": "http://www.damienduhamel.fr", - "contributions": [] + "contributions": [ + "code" + ] }, { "login": "juliankrispel", diff --git a/README.md b/README.md index 7e4a403a..20a6e778 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ These wonderful folks are responsible for developing and maintaining Netlify CMS | [
CΓ‘ssio Souza](https://twitter.com/cassiozen)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=cassiozen "Code") | [
Shawn Erquhart](https://erquh.art)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=erquhart "Code") | [
Andrey Okonetchnikov](http://okonet.ru)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=okonet "Code") | [
Mathias Biilmann](https://www.netlify.com)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=biilmann "Code") | [
Benaiah Mischenko](http://benaiah.me)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=Benaiah "Code") | [
Rafael Conde](http://rafaelconde.net)
[🎨](#design-rafaelconde "Design") [πŸ’»](https://github.com/netlify/netlify-cms/commits?author=rafaelconde "Code") | [
Joseph Earl](http://josephearl.co.uk)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=josephearl "Code") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [
Brian Douglas](https://briandouglas.me/)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=bdougie "Code") | [
Frances Berriman](https://fberriman.com)
[🎨](#design-phae "Design") | [
David Calavera](https://github.com/calavera)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=calavera "Code") | [
Luis Correia](https://luisc.pt)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=lmcorreia "Code") | [
Caleb](https://github.com/tech4him1)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=tech4him1 "Code") | [
Jerry Holmes](http://www.pagedragon.com)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=jholmes033169 "Code") | [
abumalick](https://github.com/abumalick)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=abumalick "Code") | -| [
Eamonn Bell](http://www.eamonnbell.com)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=eamonnbell "Code") | [
Jessica Parsons](https://github.com/verythorough)
[πŸ“–](https://github.com/netlify/netlify-cms/commits?author=verythorough "Documentation") | [
Kyle Mathews](https://www.bricolage.io)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=KyleAMathews "Code") | [
americool](https://github.com/americool)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=americool "Code") | [
Damien Duhamel](http://www.damienduhamel.fr)
| [
Julian Krispel-Samsel](http://reactrocket.com)
| [
Kevin McCormack](http://harlemsquirrel.github.io/)
| +| [
Eamonn Bell](http://www.eamonnbell.com)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=eamonnbell "Code") | [
Jessica Parsons](https://github.com/verythorough)
[πŸ“–](https://github.com/netlify/netlify-cms/commits?author=verythorough "Documentation") | [
Kyle Mathews](https://www.bricolage.io)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=KyleAMathews "Code") | [
americool](https://github.com/americool)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=americool "Code") | [
Damien Duhamel](http://www.damienduhamel.fr)
[πŸ’»](https://github.com/netlify/netlify-cms/commits?author=Dammmien "Code") | [
Julian Krispel-Samsel](http://reactrocket.com)
| [
Kevin McCormack](http://harlemsquirrel.github.io/)
| | [
Kalin Chernev](https://github.com/kalinchernev)
| [
tortilaman](https://github.com/tortilaman)
| [
VΓ‘clav Klecanda](http://www.vxk.cz)
| [
DrianHillman](http://drianhillman.me)
| [
Sean Crawford](https://github.com/seantcoyote)
| [
Robert Riemann](http://blog.riemann.cc)
| [
Ben Berman](http://jygabyte.com)
| | [
Benjamin Kniffler](https://github.com/bkniffler)
| [
Mike Wickett](http://www.wickett.ca)
| [
Rory Claasen](http://roryclaasen.me)
| [
Frederic Brodbeck](http://www.freder.io/)
| [
Stuart Dum](https://github.com/simplystuart)
| [
Ryan Watters](https://github.com/rdwatters)
| [
Helder S Ribeiro](https://twitter.com/hsribei)
| | [
Artem Govorov](http://dm.gl)
| [
CΓ©dric Delpoux](http://xuopled.netlify.com/)
| [
imorente](https://github.com/imorente)
| [
David Francoeur](http://davidfrancoeur.com)
| [
Rusta](https://github.com/Rusta)
| [
Henrik Lau Eriksson](http://henrik.laueriksson.com)
| [
Kraig Walker](https://www.kraigwalker.com)
| diff --git a/src/components/Editor/EditorControlPane/Widget.js b/src/components/Editor/EditorControlPane/Widget.js index 9d7f5061..45964d98 100644 --- a/src/components/Editor/EditorControlPane/Widget.js +++ b/src/components/Editor/EditorControlPane/Widget.js @@ -6,6 +6,13 @@ import ValidationErrorTypes from 'Constants/validationErrorTypes'; const truthy = () => ({ error: false }); +const isEmpty = value => ( + value === null || + value === undefined || + (value.hasOwnProperty('length') && value.length === 0) || + (value.constructor === Object && Object.keys(value).length === 0) +); + export default class Widget extends Component { static propTypes = { controlComponent: PropTypes.func.isRequired, @@ -84,14 +91,9 @@ export default class Widget extends Component { this.props.onValidate(errors); }; - validatePresence(field, value) { + validatePresence = (field, value) => { const isRequired = field.get('required', true); - if (isRequired && ( - value === null || - value === undefined || - (value.hasOwnProperty('length') && value.length === 0) || - (value.constructor === Object && Object.keys(value).length === 0) - )) { + if (isRequired && isEmpty(value)) { const error = { type: ValidationErrorTypes.PRESENCE, message: `${ field.get('label', field.get('name')) } is required.`, @@ -100,10 +102,15 @@ export default class Widget extends Component { return { error }; } return { error: false }; - } + }; - validatePattern(field, value) { + validatePattern = (field, value) => { const pattern = field.get('pattern', false); + + if (isEmpty(value)) { + return { error: false }; + } + if (pattern && !RegExp(pattern.first()).test(value)) { const error = { type: ValidationErrorTypes.PATTERN, @@ -114,7 +121,7 @@ export default class Widget extends Component { } return { error: false }; - } + }; validateWrappedControl = (field) => { const response = this.wrappedControlValid(); @@ -126,7 +133,7 @@ export default class Widget extends Component { } else if (response instanceof Promise) { response.then( () => { this.validate({ error: false }); }, - (err) => { + (err) => { const error = { type: ValidationErrorTypes.CUSTOM, message: `${ field.get('label', field.get('name')) } - ${ err }.`,