Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework: add schema for posts #3487

Merged
merged 1 commit into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion client/state/posts/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
getSerializedPostsQueryWithoutPage
} from './utils';
import { DEFAULT_POST_QUERY } from './constants';
import { itemsSchema } from './schema';
import { isValidStateWithSchema } from 'state/utils';

/**
* Tracks all known post objects, indexed by post global ID.
Expand All @@ -36,8 +38,11 @@ export function items( state = {}, action ) {
case POSTS_RECEIVE:
return Object.assign( {}, state, keyBy( action.posts, 'global_ID' ) );
case SERIALIZE:
return {};
return state;
case DESERIALIZE:
if ( isValidStateWithSchema( state, itemsSchema ) ) {
return state;
}
return {};
}
return state;
Expand Down
53 changes: 53 additions & 0 deletions client/state/posts/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export const itemsSchema = {
type: 'object',
patternProperties: {
//be careful to escape regexes properly
'^[0-9a-z]+$': {
type: 'object',
required: [ 'ID', 'site_ID', 'global_ID' ],
properties: {
ID: { type: 'integer' },
site_ID: { type: 'integer' },
global_ID: { type: 'string' },
author: { type: 'object' },
date: { type: 'string' },
modified: { type: 'string' },
title: { type: 'string' },
URL: { type: 'string' },
short_URL: { type: 'string' },
content: { type: 'string' },
excerpt: { type: 'string' },
slug: { type: 'string' },
guid: { type: 'string' },
status: { type: 'string' },
sticky: { type: 'boolean' },
password: { type: 'string' },
parent: { type: [ 'object', 'boolean' ] },
type: { type: 'string' },
discussion: { type: 'object' },
likes_enabled: { type: 'boolean' },
sharing_enabled: { type: 'boolean' },
like_count: { type: 'integer' },
i_like: { type: 'boolean' },
is_reblogged: { type: 'boolean' },
is_following: { type: 'boolean' },
featured_image: { type: 'string' },
post_thumbnail: { type: [ 'string', 'null' ] },
format: { type: 'string' },
geo: { type: 'boolean' },
menu_order: { type: 'integer' },
page_template: { type: 'string' },
publicize_URLS: { type: 'array' },
tags: { type: 'object' },
categories: { type: 'object' },
attachments: { type: 'object' },
attachment_count: { type: 'integer' },
metadata: { type: 'array' },
meta: { type: 'object' },
capabilities: { type: 'object' },
other_URLs: { type: 'object' }
}
}
},
additionalProperties: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering, should we allow additionalProperties here? What if we wanted to add a property for some other client... that wouldn't necessarily get a lot of testing before the change hit the API because we've traditionally been pretty lax about adding properties to api responses. I can understand we don't want people to rely upon attributes outside of this schema... is there another way to enforce that without making it fail out when there's an extra attribute?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additionalProperties is by default true, and the post object itself allows extra props. This one here will not validate if it doesn't match the regex in patternProperties (a global post id like 3d097cb7c5473c169bba0eb8e3c6cb64).

};
69 changes: 51 additions & 18 deletions client/state/posts/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { expect } from 'chai';
import deepFreeze from 'deep-freeze';
import sinon from 'sinon';

/**
* Internal dependencies
Expand Down Expand Up @@ -77,20 +78,52 @@ describe( 'reducer', () => {
} );
} );

it( 'never persists state because this is not implemented', () => {
const original = deepFreeze( {
'3d097cb7c5473c169bba0eb8e3c6cb64': { ID: 841, site_ID: 2916284, global_ID: '3d097cb7c5473c169bba0eb8e3c6cb64', title: 'Hello World' }
describe( 'persistence', () => {
before( () => {
sinon.stub( console, 'warn' );
} );
after( () => {
console.warn.restore();
} );

it( 'persists state', () => {
const original = deepFreeze( {
'3d097cb7c5473c169bba0eb8e3c6cb64': {
ID: 841,
site_ID: 2916284,
global_ID: '3d097cb7c5473c169bba0eb8e3c6cb64',
title: 'Hello World'
}
} );
const state = items( original, { type: SERIALIZE } );
expect( state ).to.eql( original );
} );

it( 'loads valid persisted state', () => {
const original = deepFreeze( {
'3d097cb7c5473c169bba0eb8e3c6cb64': {
ID: 841,
site_ID: 2916284,
global_ID: '3d097cb7c5473c169bba0eb8e3c6cb64',
title: 'Hello World'
}
} );
const state = items( original, { type: DESERIALIZE } );
expect( state ).to.eql( original );
} );

it( 'loads default state when schema does not match', () => {
const original = deepFreeze( {
'3d097cb7c5473c169bba0eb8e3c6cb64': {
ID: 841,
site_ID: 'foo',
global_ID: '3d097cb7c5473c169bba0eb8e3c6cb64',
title: 'Hello World'
}
} );
const state = items( original, { type: DESERIALIZE } );
expect( state ).to.eql( {} );
} );
const state = items( original, { type: SERIALIZE } );
expect( state ).to.eql( {} );
} );

it( 'never loads persisted state because this is not implemented', () => {
const original = deepFreeze( {
'3d097cb7c5473c169bba0eb8e3c6cb64': { ID: 841, site_ID: 2916284, global_ID: '3d097cb7c5473c169bba0eb8e3c6cb64', title: 'Hello World' }
} );
const state = items( original, { type: DESERIALIZE } );
expect( state ).to.eql( {} );
} );
} );

Expand Down Expand Up @@ -215,7 +248,7 @@ describe( 'reducer', () => {
expect( state ).to.eql( {} );
} );

it( 'never loads persisted state because this is not implemented', () => {
it( 'never persists state because this is not implemented', () => {
const original = deepFreeze( {
'2916284:{"search":"hello"}': {
fetching: true
Expand Down Expand Up @@ -314,15 +347,15 @@ describe( 'reducer', () => {
} );
} );

it( 'never persists state because this is not implemented', () => {
it( 'never persists state', () => {
const original = deepFreeze( {
'2916284:{"search":"hello"}': 1
} );
const state = queriesLastPage( original, { type: SERIALIZE } );
expect( state ).to.eql( {} );
} );

it( 'never loads persisted state because this is not implemented', () => {
it( 'never loads persisted state', () => {
const original = deepFreeze( {
'2916284:{"search":"hello"}': 1
} );
Expand Down Expand Up @@ -407,7 +440,7 @@ describe( 'reducer', () => {
} );
} );

it( 'never persists state because this is not implemented', () => {
it( 'never persists state', () => {
const state = siteRequests( deepFreeze( {
2916284: {
841: true
Expand All @@ -419,7 +452,7 @@ describe( 'reducer', () => {
expect( state ).to.eql( {} );
} );

it( 'never loads persisted state because this is not implemented', () => {
it( 'never loads persisted state', () => {
const state = siteRequests( deepFreeze( {
2916284: {
841: true
Expand Down