Skip to content

Commit 256a638

Browse files
authored
Merge pull request #20 from Feverup/feature/PNW-2637_negative-pattern
PNW-2637 - add negative pattern feature
2 parents f1d38d2 + 84c397b commit 256a638

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

packages/decap-cms-core/src/components/Editor/EditorControlPane/Widget.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export default class Widget extends Component {
127127
const value = this.getValidateValue();
128128
const field = this.props.field;
129129
const errors = [];
130-
const validations = [this.validatePresence, this.validatePattern];
130+
const validations = [this.validatePresence, this.validatePattern, this.validateNegativePattern];
131131
if (field.get('meta')) {
132132
validations.push(this.props.validateMetaField);
133133
}
@@ -186,6 +186,30 @@ export default class Widget extends Component {
186186
return { error: false };
187187
};
188188

189+
validateNegativePattern = (field, value) => {
190+
const { t, parentIds } = this.props;
191+
const negativePattern = field.get('negative_pattern', false);
192+
193+
if (isEmpty(value)) {
194+
return { error: false };
195+
}
196+
197+
if (negativePattern && RegExp(negativePattern.first()).test(value)) {
198+
const error = {
199+
type: ValidationErrorTypes.PATTERN,
200+
parentIds,
201+
message: t('editor.editorControlPane.widget.regexNegativePattern', {
202+
fieldLabel: field.get('label', field.get('name')),
203+
pattern: negativePattern.last(),
204+
}),
205+
};
206+
207+
return { error };
208+
}
209+
210+
return { error: false };
211+
};
212+
189213
validateWrappedControl = field => {
190214
const { t, parentIds } = this.props;
191215
if (typeof this.wrappedControlValid !== 'function') {
@@ -255,7 +279,7 @@ export default class Widget extends Component {
255279

256280
setInactiveStyle = () => {
257281
this.props.setInactiveStyle();
258-
if (this.props.field.has('pattern') && !isEmpty(this.getValidateValue())) {
282+
if ((this.props.field.has('pattern') || this.props.field.has('negative_pattern')) && !isEmpty(this.getValidateValue())) {
259283
this.validate();
260284
}
261285
};

packages/decap-cms-core/src/constants/configSchema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ function fieldsConfig() {
6666
minItems: 2,
6767
items: [{ oneOf: [{ type: 'string' }, { instanceof: 'RegExp' }] }, { type: 'string' }],
6868
},
69+
negative_pattern: {
70+
type: 'array',
71+
minItems: 2,
72+
items: [{ oneOf: [{ type: 'string' }, { instanceof: 'RegExp' }] }, { type: 'string' }],
73+
},
6974
field: { $ref: `field_${id}` },
7075
fields: { $ref: `fields_${id}` },
7176
types: { $ref: `fields_${id}` },

packages/decap-cms-locales/src/en/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const en = {
8080
widget: {
8181
required: '%{fieldLabel} is required.',
8282
regexPattern: "%{fieldLabel} didn't match the pattern: %{pattern}.",
83+
regexNegativePattern: "%{fieldLabel} should not contain pattern: %{pattern}.",
8384
processing: '%{fieldLabel} is processing.',
8485
range: '%{fieldLabel} must be between %{minValue} and %{maxValue}.',
8586
min: '%{fieldLabel} must be at least %{minValue}.',
@@ -139,7 +140,7 @@ const en = {
139140
saving: 'Saving...',
140141
save: 'Save',
141142
statusInfoTooltipDraft:
142-
'Entry status is set to draft. To finalize and submit it for review, set the status to In review',
143+
'Entry status is set to draft. To finalize and submit it for review, set the status to "In review"',
143144
statusInfoTooltipInReview:
144145
'Entry is being reviewed, no further actions are required. However, you can still make additional changes while it is being reviewed.',
145146
discarding: 'Discarding...',

0 commit comments

Comments
 (0)