Skip to content

Commit

Permalink
Merge pull request #220 from Inist-CNRS/uri_must_be_first
Browse files Browse the repository at this point in the history
[RFR] Ensure URI is always the first field
  • Loading branch information
ThieryMichel authored Mar 30, 2017
2 parents d3adfaa + 7ef844c commit 1e188d2
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/api/models/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ export default async (db) => {

collection.addContributionField = async (field, contributor, isLogged, nameArg) => {
const name = field.name || nameArg || await generateUid();
const position = await collection.count();
await validateField({
...field,
cover: COVER_DOCUMENT,
name,
position,
}, true);

if (!field.name) {
Expand All @@ -115,15 +117,13 @@ export default async (db) => {
name,
cover: COVER_DOCUMENT,
contribution: true,
position,
};
if (!isLogged) {
fieldData.contributors = [contributor];
}

await collection.insertOne({
...fieldData,
name,
});
await collection.insertOne(fieldData);

return name;
}
Expand Down
13 changes: 13 additions & 0 deletions src/api/models/field.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('field', () => {
name: 'nameArg',
cover: COVER_DOCUMENT,
contribution: true,
position: 10,
});
});

Expand All @@ -109,6 +110,7 @@ describe('field', () => {
cover: COVER_DOCUMENT,
contribution: true,
contributors: [contributor],
position: 10,
});
});

Expand Down Expand Up @@ -202,6 +204,7 @@ describe('field', () => {
label: 'label',
name: 'name',
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN', args: [{ value: 'a' }] },
],
Expand All @@ -215,6 +218,7 @@ describe('field', () => {
label: 'label',
name: 'name',
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [],
};
expect(() => validateField(field))
Expand All @@ -227,6 +231,7 @@ describe('field', () => {
label: 'label',
name: 'name',
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN', args: ['a'] },
],
Expand All @@ -241,6 +246,7 @@ describe('field', () => {
label: 'label',
name: 'name',
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN', args: ['a'] },
],
Expand All @@ -255,6 +261,7 @@ describe('field', () => {
name: 'name',
label: undefined,
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN', args: ['a'] },
],
Expand All @@ -269,6 +276,7 @@ describe('field', () => {
label: 'la',
name: 'name',
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN', args: ['a'] },
],
Expand All @@ -283,6 +291,7 @@ describe('field', () => {
label: 'label',
name: 'name',
scheme: 'ftp://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN', args: ['a'] },
],
Expand All @@ -297,6 +306,7 @@ describe('field', () => {
label: 'label',
name: 'name',
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN' },
],
Expand All @@ -311,6 +321,7 @@ describe('field', () => {
label: 'label',
name: 'name',
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN', args: [] },
{ operation: 'UNKNOWN', args: [] },
Expand All @@ -325,6 +336,7 @@ describe('field', () => {
cover: 'document',
label: 'label',
name: 'name',
position: 1,
scheme: 'http://purl.org/dc/terms/title',
};
expect(validateField(field, true)).toEqual(field);
Expand All @@ -336,6 +348,7 @@ describe('field', () => {
label: 'label',
name: 'name',
scheme: 'http://purl.org/dc/terms/title',
position: 1,
transformers: [
{ operation: 'COLUMN', args: [] },
],
Expand Down
9 changes: 6 additions & 3 deletions src/app/e2e/admin/composedOf.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"format": {
"name": "uri"
},
"scheme": "http://uri4uri.net/vocab#URI"
"scheme": "http://uri4uri.net/vocab#URI",
"position": 0
},
{
"name": "name",
Expand All @@ -40,7 +41,8 @@
"operation": "UPPERCASE",
"args": []
}
]
],
"position": 1
},
{
"name": "firstname",
Expand All @@ -64,7 +66,8 @@
"operation": "UPPERCASE",
"args": []
}
]
],
"position": 2
}
],
"dataset": [
Expand Down
27 changes: 18 additions & 9 deletions src/app/e2e/resource_published.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"format": {
"name": "uri"
},
"scheme": "http://uri4uri.net/vocab#URI"
"scheme": "http://uri4uri.net/vocab#URI",
"position": 0
}, {
"name": "name",
"label": "name",
Expand All @@ -38,7 +39,8 @@
"operation": "UPPERCASE",
"args": []
}
]
],
"position": 1
}, {
"name": "fullname",
"label": "Full name",
Expand All @@ -53,7 +55,8 @@
"firstname",
"name"
]
}
},
"position": 2
}, {
"name": "firstname",
"label": "firstname",
Expand All @@ -76,7 +79,8 @@
"operation": "UPPERCASE",
"args": []
}
]
],
"position": 3
}, {
"name": "email",
"label": "Email",
Expand All @@ -97,7 +101,8 @@
]
}
],
"completes": "fullname"
"completes": "fullname",
"position": 4
}, {
"name": "best_friend_of",
"label": "Best Friend Of",
Expand Down Expand Up @@ -129,7 +134,8 @@
"type": "column"
},
"name": "uri"
}
},
"position": 5
}, {
"name": "movie",
"label": "Movie",
Expand All @@ -145,7 +151,8 @@
}
]
}
]
],
"position": 6
}, {
"name": "author",
"label": "Author",
Expand All @@ -162,7 +169,8 @@
]
}
],
"completes": "movie"
"completes": "movie",
"position": 7
},{
"label": "note",
"name": "note",
Expand All @@ -173,7 +181,8 @@
"name": "john",
"mail": "[email protected]"
}
]
],
"position": 8
}
],
"publishedCharacteristic": [
Expand Down
7 changes: 0 additions & 7 deletions src/app/js/admin/fields/PositionField.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ export const PositionFieldComponent = ({ field, fields, p: polyglot, ...props })
fullWidth
{...props}
>
<MenuItem
className="first_position"
key="first_position"
value={0}
primaryText={polyglot.t('first_position')}
/>

{fieldItems}
</Field>
);
Expand Down
7 changes: 3 additions & 4 deletions src/app/js/i18n/translations/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ export default {
error_label_required: 'Label is required',
error_cover_required: 'Cover is required',
error_scheme_invalid: 'scheme must be an url',
error_transformers_required_or_composed_of_required: 'Operations are required (except if composedOf is set)',
error_position_invalid: 'Position is invalid (must be greater than zero)',
error_position_required: 'Position is required',
error_position_uri_must_come_first: 'URI field must be the first',
error_transformers_contributions_no_transformers: 'A contribution cannot have an operation',
error_transformers_composed_of_conflict: 'Operations cannot be set at the same time as composedOf',
error_composedOf_required_or_transformers_required: 'ComposedOf is required (except if there are operations)',
error_composedOf_transformers_conflict: 'ComposedOf cannot be set at the same time as operations',
error_composedOf_contribution_no_composed_of: 'A contribution cannot have a composedOf attribute',
'error_composedOf.separator_required': 'ComposedOf separator is required',
'error_composedOf.separator_invalid': 'ComposedOf separator must be a string',
Expand Down
42 changes: 42 additions & 0 deletions src/common/validateFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,47 @@ export const validateCover = (field, isContribution) => {
return result;
};

export const validatePosition = (field) => {
const result = {
name: 'position',
isValid: true,
};

if (typeof field.position === 'undefined' || field.position === null) {
return {
...result,
isValid: false,
error: 'required',
};
}

if (field.position < 0) {
return {
...result,
isValid: false,
error: 'invalid',
};
}

if (field.position === 0 && field.name !== 'uri') {
return {
...result,
isValid: false,
error: 'uri_must_come_first',
};
}

if (field.position > 0 && field.name === 'uri') {
return {
...result,
isValid: false,
error: 'uri_must_come_first',
};
}

return result;
};

export const validateTransformers = (field, isContribution) => {
const result = {
name: 'transformers',
Expand Down Expand Up @@ -268,6 +309,7 @@ export const validateField = (field, isContribution = false, fields = []) => {
validateLabel(field),
validateCover(field, isContribution),
validateScheme(field),
validatePosition(field),
validateTransformers(field, isContribution),
validateCompletesField(field, fields),
validateComposedOf(field, isContribution),
Expand Down
Loading

0 comments on commit 1e188d2

Please sign in to comment.