Skip to content

Commit

Permalink
refactor: move args validation to template helper
Browse files Browse the repository at this point in the history
  • Loading branch information
buschtoens committed Apr 26, 2019
1 parent 529681c commit 37fb10e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
26 changes: 1 addition & 25 deletions addon/components/link/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { reads } from '@ember/object/computed';
import Component from '@glimmer/component';
import { assert } from '@ember/debug';

function isQueryParams(
maybeQueryParam: any
Expand All @@ -20,7 +19,7 @@ type RouteModel = object | string | number;

type QueryParams = Record<string, any>;

interface LinkArgs {
export interface LinkArgs {
/**
* The target route name.
*/
Expand Down Expand Up @@ -60,29 +59,6 @@ export default class LinkComponent extends Component<LinkArgs> {
// @ts-ignore
private currentURL!: string;

didUpdate() {
super.didUpdate();

assert(
`You provided '@queryParams', but the argument you mean is just '@query'.`,
!('queryParams' in this.args)
);
assert(
`You provided '@routeName', but the argument you mean is just '@route'.`,
!('routeName' in this.args)
);
assert(
`'@route' needs to be a valid route name.`,
typeof this.args.route === 'string'
);
assert(
`You cannot use both '@model' ('${this.args.model}') and '@models' ('${
this.args.models
}') at the same time.`,
!(this.args.model && this.args.models)
);
}

private get modelsAndQueryParams(): [RouteModel[], null | QueryParams] {
const { model, models, query } = this.args;
if (models) {
Expand Down
6 changes: 4 additions & 2 deletions addon/components/link/template.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{yield
{{~ember-link/validate-args this.args~}}

{{~yield
(hash
href=this.href
isActive=this.isActive
Expand All @@ -7,4 +9,4 @@
transitionTo=this.transitionTo
replaceWith=this.replaceWith
)
}}
~}}
26 changes: 26 additions & 0 deletions addon/helpers/ember-link/validate-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { helper } from '@ember/component/helper';
import { assert } from '@ember/debug';
import { LinkArgs } from 'ember-link/components/link/component';

export function emberLinkValidateArgs([args]: [LinkArgs]): void {
assert(
`You provided '@queryParams', but the argument you mean is just '@query'.`,
!('queryParams' in args)
);
assert(
`You provided '@routeName', but the argument you mean is just '@route'.`,
!('routeName' in args)
);
assert(
`'@route' needs to be a valid route name.`,
typeof args.route === 'string'
);
assert(
`You cannot use both '@model' ('${args.model}') and '@models' ('${
args.models
}') at the same time.`,
!(args.model && args.models)
);
}

export default helper(emberLinkValidateArgs);
4 changes: 4 additions & 0 deletions app/helpers/ember-link/validate-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
default,
emberLinkValidateArgs
} from 'ember-link/helpers/ember-link/validate-args';

0 comments on commit 37fb10e

Please sign in to comment.