Skip to content

Commit

Permalink
fix(validations): Add guards for potentially undefined field (#22)
Browse files Browse the repository at this point in the history
* Add guards for potentially undefined field

* Add render test
  • Loading branch information
hanna-greaves authored Sep 9, 2020
1 parent 2a7cda9 commit fb4dcec
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cf-property-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {renderUnionType} from './renderer/render-union-type';
import {Field} from 'contentful';

export const anyType = (field: Field): string => {
if (field.validations.length > 0) {
if (field.validations?.length > 0) {
const includesValidation = field.validations.find(validation => validation.in);
if (includesValidation && includesValidation.in) {
const mapper = (): (value: string) => string => {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/cf-render-prop-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const renderPropArray = (field: Field): string => {
if (field.items.type === 'Symbol') {
const validation = inValidations(field.items);

if (validation.length > 0) {
if (validation?.length > 0) {
return `(${renderUnionType(validation.map(renderLiteralType))})[]`;
}
return 'Contentful.EntryFields.Symbol[]';
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/cf-render-prop-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {renderUnionType} from './render-union-type';

const linkContentType = (field: Pick<Field, 'id' | 'validations'>): string => {
const validations = linkContentTypeValidations(field);
return renderUnionType(validations.length > 0 ? validations.map(moduleFieldsName) : ['any']);
return renderUnionType(validations?.length > 0 ? validations.map(moduleFieldsName) : ['any']);
};

export const renderPropLink = (field: Pick<Field, 'id' | 'validations' | 'linkType'>) => {
const value = field.validations.length === 0 ? 'any' : linkContentType(field);
const value = field.validations && field.validations.length === 0 ? 'any' : linkContentType(field);
return field.linkType === 'Entry'
? renderGenericType('Contentful.' + field.linkType, value)
: 'Contentful.' + field.linkType!;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const moduleFieldsName = (name: string): string => moduleName(name) + 'Fi
type WithValidations = Pick<FieldItem, 'validations'>;

const validation = (node: WithValidations, field: keyof FieldValidation): any => {
if (node.validations.length !== 0) {
if (node.validations && node.validations.length !== 0) {
const linkContentValidation = node.validations.find(value => value[field]);
if (linkContentValidation) {
return linkContentValidation[field] || [];
Expand Down
20 changes: 18 additions & 2 deletions test/cf-render-prop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { renderProp } from '../src/renderer/cf-render-prop';
import {expect} from '@oclif/test';

describe('A renderProp function', () => {
it('can evaluate an "Symbol" type', () => {
it('can evaluate a "Symbol" type', () => {
const field = JSON.parse(`
{
"id": "internalName",
Expand All @@ -20,7 +20,7 @@ describe('A renderProp function', () => {
expect(renderProp(field)).to.equal('Contentful.EntryFields.Symbol');
});

it('can evaluate an "Symbol" type with "in" validation', () => {
it('can evaluate a "Symbol" type with "in" validation', () => {
const field = JSON.parse(`
{
"id": "headerAlignment",
Expand All @@ -44,6 +44,22 @@ describe('A renderProp function', () => {
expect(renderProp(field)).to.equal('"Left-aligned" | "Center-aligned"');
});

it('can evaluate a "Symbol" type with missing validations', () => {
const field = JSON.parse(`
{
"id": "internalName",
"name": "Internal name",
"type": "Symbol",
"localized": false,
"required": false,
"disabled": false,
"omitted": false
}
`);

expect(renderProp(field)).to.equal('Contentful.EntryFields.Symbol');
});

it('can evaluate a "Link" type', () => {
const field = JSON.parse(`
{
Expand Down

0 comments on commit fb4dcec

Please sign in to comment.