mirror of
https://github.com/strapi/strapi.git
synced 2025-11-03 19:36:20 +00:00
Fix data types output to be the same wathever the database is
This commit is contained in:
parent
d1ee6a662e
commit
b35712acc5
@ -876,25 +876,16 @@ const castValueFromType = (type, value, definition) => {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
// TODO: handle real date format 1970-01-01
|
// TODO: handle real date format 1970-01-01
|
||||||
case 'date':
|
|
||||||
case 'datetime': {
|
|
||||||
const date = dateFns.parse(value);
|
|
||||||
|
|
||||||
if (!dateFns.isValid(date)) {
|
|
||||||
throw new Error(
|
|
||||||
`Invalid ${type} format, expected a timestamp or an ISO date`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
// TODO: handle real time format 12:00:00
|
// TODO: handle real time format 12:00:00
|
||||||
case 'time':
|
case 'time':
|
||||||
case 'timestamp': {
|
case 'timestamp':
|
||||||
|
case 'date':
|
||||||
|
case 'datetime': {
|
||||||
const date = dateFns.parse(value);
|
const date = dateFns.parse(value);
|
||||||
if (dateFns.isValid(date)) return date;
|
if (dateFns.isValid(date)) return date;
|
||||||
|
|
||||||
date.setTime(value);
|
date.setTime(value);
|
||||||
|
|
||||||
if (!dateFns.isValid(date)) {
|
if (!dateFns.isValid(date)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Invalid ${type} format, expected a timestamp or an ISO date`
|
`Invalid ${type} format, expected a timestamp or an ISO date`
|
||||||
|
|||||||
@ -179,6 +179,11 @@ module.exports = strapi => {
|
|||||||
options.connection.supportBigNumbers = true;
|
options.connection.supportBigNumbers = true;
|
||||||
options.connection.bigNumberStrings = true;
|
options.connection.bigNumberStrings = true;
|
||||||
options.connection.typeCast = (field, next) => {
|
options.connection.typeCast = (field, next) => {
|
||||||
|
if (field.type == 'DECIMAL' || field.type === 'NEWDECIMAL') {
|
||||||
|
var value = field.string();
|
||||||
|
return value === null ? null : Number(value);
|
||||||
|
}
|
||||||
|
|
||||||
if (field.type == 'TINY' && field.length == 1) {
|
if (field.type == 'TINY' && field.length == 1) {
|
||||||
let value = field.string();
|
let value = field.string();
|
||||||
return value ? value == '1' : null;
|
return value ? value == '1' : null;
|
||||||
|
|||||||
@ -45,18 +45,7 @@ describe('Test type boolean', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Set null on invalid boolean value', async () => {
|
test.todo('Throws on invalid boolean value');
|
||||||
const res = await rq.post('/content-manager/explorer/withboolean', {
|
|
||||||
formData: {
|
|
||||||
data: JSON.stringify({ field: 'random' }),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body).toMatchObject({
|
|
||||||
field: null,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Convert integer to boolean value', async () => {
|
test('Convert integer to boolean value', async () => {
|
||||||
let res = await rq.post('/content-manager/explorer/withboolean', {
|
let res = await rq.post('/content-manager/explorer/withboolean', {
|
||||||
|
|||||||
@ -20,22 +20,20 @@ describe('Test type date', () => {
|
|||||||
}, 60000);
|
}, 60000);
|
||||||
|
|
||||||
test('Create entry with valid value JSON', async () => {
|
test('Create entry with valid value JSON', async () => {
|
||||||
const now = new Date();
|
|
||||||
|
|
||||||
const res = await rq.post('/content-manager/explorer/withdate', {
|
const res = await rq.post('/content-manager/explorer/withdate', {
|
||||||
body: {
|
body: {
|
||||||
field: now,
|
field: '2019-08-08T10:10:57.000Z',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.body).toMatchObject({
|
expect(res.body).toMatchObject({
|
||||||
field: now.toISOString(),
|
field: '2019-08-08T10:10:57.000Z',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Create entry with valid value FormData', async () => {
|
test('Create entry with valid value FormData', async () => {
|
||||||
const now = new Date();
|
const now = new Date(2019, 0, 12);
|
||||||
|
|
||||||
const res = await rq.post('/content-manager/explorer/withdate', {
|
const res = await rq.post('/content-manager/explorer/withdate', {
|
||||||
formData: {
|
formData: {
|
||||||
@ -50,7 +48,7 @@ describe('Test type date', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Create entry with timestamp value should be converted to ISO', async () => {
|
test('Create entry with timestamp value should be converted to ISO', async () => {
|
||||||
const now = new Date();
|
const now = new Date(2016, 4, 8);
|
||||||
|
|
||||||
const res = await rq.post('/content-manager/explorer/withdate', {
|
const res = await rq.post('/content-manager/explorer/withdate', {
|
||||||
body: {
|
body: {
|
||||||
@ -64,8 +62,8 @@ describe('Test type date', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Throws on invalid date format', async () => {
|
test('Accepts string timestamp', async () => {
|
||||||
const now = new Date();
|
const now = new Date(2000, 0, 1);
|
||||||
|
|
||||||
const res = await rq.post('/content-manager/explorer/withdate', {
|
const res = await rq.post('/content-manager/explorer/withdate', {
|
||||||
body: {
|
body: {
|
||||||
@ -73,6 +71,19 @@ describe('Test type date', () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toMatchObject({
|
||||||
|
field: now.toISOString(),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Throws on invalid date format', async () => {
|
||||||
|
const res = await rq.post('/content-manager/explorer/withdate', {
|
||||||
|
body: {
|
||||||
|
field: 'azdazindoaizdnoainzd',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
expect(res.statusCode).toBe(400);
|
expect(res.statusCode).toBe(400);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -87,7 +98,7 @@ describe('Test type date', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Updating entry sets the right value and format JSON', async () => {
|
test('Updating entry sets the right value and format JSON', async () => {
|
||||||
const now = new Date();
|
const now = new Date(2018, 7, 5);
|
||||||
|
|
||||||
const res = await rq.post('/content-manager/explorer/withdate', {
|
const res = await rq.post('/content-manager/explorer/withdate', {
|
||||||
body: {
|
body: {
|
||||||
@ -95,7 +106,7 @@ describe('Test type date', () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const newDate = new Date();
|
const newDate = new Date(2017, 10, 23);
|
||||||
const updateRes = await rq.put(
|
const updateRes = await rq.put(
|
||||||
`/content-manager/explorer/withdate/${res.body.id}`,
|
`/content-manager/explorer/withdate/${res.body.id}`,
|
||||||
{
|
{
|
||||||
|
|||||||
@ -63,7 +63,7 @@ const productFixtures = [
|
|||||||
price: 10.99,
|
price: 10.99,
|
||||||
decimal_field: 42.43,
|
decimal_field: 42.43,
|
||||||
rank: 42,
|
rank: 42,
|
||||||
big_rank: 345678912983,
|
big_rank: '345678912983',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Product 2',
|
name: 'Product 2',
|
||||||
@ -71,7 +71,7 @@ const productFixtures = [
|
|||||||
price: 28.31,
|
price: 28.31,
|
||||||
decimal_field: 91.22,
|
decimal_field: 91.22,
|
||||||
rank: 82,
|
rank: 82,
|
||||||
big_rank: 926371623421,
|
big_rank: '926371623421',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Product 3',
|
name: 'Product 3',
|
||||||
@ -79,7 +79,7 @@ const productFixtures = [
|
|||||||
price: 28.31,
|
price: 28.31,
|
||||||
decimal_field: 12.22,
|
decimal_field: 12.22,
|
||||||
rank: 91,
|
rank: 91,
|
||||||
big_rank: 926372323421,
|
big_rank: '926372323421',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Product 4',
|
name: 'Product 4',
|
||||||
@ -87,7 +87,7 @@ const productFixtures = [
|
|||||||
price: null,
|
price: null,
|
||||||
decimal_field: 12.22,
|
decimal_field: 12.22,
|
||||||
rank: 99,
|
rank: 99,
|
||||||
big_rank: 999999999999,
|
big_rank: '999999999999',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -605,7 +605,7 @@ describe('Filtering API', () => {
|
|||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: '/products',
|
url: '/products',
|
||||||
qs: {
|
qs: {
|
||||||
big_rank_gte: 345678912983,
|
big_rank_gte: '345678912983',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -753,7 +753,7 @@ describe('Filtering API', () => {
|
|||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: '/products',
|
url: '/products',
|
||||||
qs: {
|
qs: {
|
||||||
big_rank_lte: 345678912983,
|
big_rank_lte: '345678912983',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user