Merge branch 'master' of github.com:keen-studio/payload
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
const { MissingFieldType } = require('../errors');
|
||||
const { MissingFieldType, InvalidFieldRelationship } = require('../errors');
|
||||
const validations = require('./validations');
|
||||
|
||||
const sanitizeFields = (fields) => {
|
||||
const sanitizeFields = (fields, validRelationships) => {
|
||||
if (!fields) return [];
|
||||
|
||||
return fields.map((unsanitizedField) => {
|
||||
@@ -9,6 +9,15 @@ const sanitizeFields = (fields) => {
|
||||
|
||||
if (!field.type) throw new MissingFieldType(field);
|
||||
|
||||
if (field.type === 'relationship') {
|
||||
const relationships = Array.isArray(field.relationTo) ? field.relationTo : [field.relationTo];
|
||||
relationships.forEach((relationship) => {
|
||||
if (!validRelationships.includes(relationship)) {
|
||||
throw new InvalidFieldRelationship(field, relationship);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof field.validate === 'undefined') {
|
||||
const defaultValidate = validations[field.type];
|
||||
const noValidate = () => true;
|
||||
@@ -24,7 +33,7 @@ const sanitizeFields = (fields) => {
|
||||
if (field.blocks) {
|
||||
field.blocks = field.blocks.map((block) => {
|
||||
const unsanitizedBlock = { ...block };
|
||||
unsanitizedBlock.fields = sanitizeFields(block.fields);
|
||||
unsanitizedBlock.fields = sanitizeFields(block.fields, validRelationships);
|
||||
return unsanitizedBlock;
|
||||
});
|
||||
}
|
||||
|
||||
111
src/fields/sanitize.spec.js
Normal file
111
src/fields/sanitize.spec.js
Normal file
@@ -0,0 +1,111 @@
|
||||
/* eslint-disable jest/require-to-throw-message */
|
||||
const sanitizeFields = require('./sanitize');
|
||||
const { MissingFieldType, InvalidFieldRelationship } = require('../errors');
|
||||
|
||||
describe('sanitizeFields', () => {
|
||||
it('should throw on missing type field', () => {
|
||||
const fields = [{
|
||||
label: 'some-collection',
|
||||
name: 'Some Collection',
|
||||
}];
|
||||
expect(() => {
|
||||
sanitizeFields(fields, []);
|
||||
}).toThrow(MissingFieldType);
|
||||
});
|
||||
|
||||
describe('relationships', () => {
|
||||
it('should not throw on valid relationship', () => {
|
||||
const validRelationships = ['some-collection'];
|
||||
const fields = [{
|
||||
type: 'relationship',
|
||||
label: 'my-relationship',
|
||||
name: 'My Relationship',
|
||||
relationTo: 'some-collection',
|
||||
}];
|
||||
expect(() => {
|
||||
sanitizeFields(fields, validRelationships);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should not throw on valid relationship - multiple', () => {
|
||||
const validRelationships = ['some-collection', 'another-collection'];
|
||||
const fields = [{
|
||||
type: 'relationship',
|
||||
label: 'my-relationship',
|
||||
name: 'My Relationship',
|
||||
relationTo: ['some-collection', 'another-collection'],
|
||||
}];
|
||||
expect(() => {
|
||||
sanitizeFields(fields, validRelationships);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should not throw on valid relationship inside blocks', () => {
|
||||
const validRelationships = ['some-collection'];
|
||||
const fields = [{
|
||||
name: 'layout',
|
||||
label: 'Layout Blocks',
|
||||
singularLabel: 'Block',
|
||||
type: 'blocks',
|
||||
blocks: [{
|
||||
fields: [{
|
||||
type: 'relationship',
|
||||
label: 'my-relationship',
|
||||
name: 'My Relationship',
|
||||
relationTo: 'some-collection',
|
||||
}],
|
||||
}],
|
||||
}];
|
||||
expect(() => {
|
||||
sanitizeFields(fields, validRelationships);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should throw on invalid relationship', () => {
|
||||
const validRelationships = ['some-collection'];
|
||||
const fields = [{
|
||||
type: 'relationship',
|
||||
label: 'my-relationship',
|
||||
name: 'My Relationship',
|
||||
relationTo: 'not-valid',
|
||||
}];
|
||||
expect(() => {
|
||||
sanitizeFields(fields, validRelationships);
|
||||
}).toThrow(InvalidFieldRelationship);
|
||||
});
|
||||
|
||||
it('should throw on invalid relationship - multiple', () => {
|
||||
const validRelationships = ['some-collection', 'another-collection'];
|
||||
const fields = [{
|
||||
type: 'relationship',
|
||||
label: 'my-relationship',
|
||||
name: 'My Relationship',
|
||||
relationTo: ['some-collection', 'not-valid'],
|
||||
}];
|
||||
expect(() => {
|
||||
sanitizeFields(fields, validRelationships);
|
||||
}).toThrow(InvalidFieldRelationship);
|
||||
});
|
||||
|
||||
it('should throw on invalid relationship inside blocks', () => {
|
||||
const validRelationships = ['some-collection'];
|
||||
const fields = [{
|
||||
name: 'layout',
|
||||
label: 'Layout Blocks',
|
||||
singularLabel: 'Block',
|
||||
type: 'blocks',
|
||||
blocks: [{
|
||||
fields: [{
|
||||
type: 'relationship',
|
||||
label: 'my-relationship',
|
||||
name: 'My Relationship',
|
||||
relationTo: 'not-valid',
|
||||
}],
|
||||
}],
|
||||
}];
|
||||
expect(() => {
|
||||
sanitizeFields(fields, validRelationships);
|
||||
}).toThrow(InvalidFieldRelationship);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -115,7 +115,7 @@ const optionsToValidatorMap = {
|
||||
return true;
|
||||
},
|
||||
date: (value, options = {}) => {
|
||||
if (value && value instanceof Date) {
|
||||
if (value && !isNaN(Date.parse(value.toString()))) { /* eslint-disable-line */
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user