Skip to content

Commit

Permalink
removed useless typecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiamanzati committed Jun 13, 2017
1 parent 30cf369 commit 798f76f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/types/utility-types/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export class IdentifierType<T> extends Type<T, T> {
return fail(`Identifier types can only be instantiated as direct child of a model type`)

if (parent.identifierAttribute) fail(`Cannot define property '${subpath}' as object identifier, property '${parent.identifierAttribute}' is already defined as identifier property`)

typecheck(this.identifierType, snapshot)
parent.identifierAttribute = subpath
return createNode(this, parent, subpath, environment, snapshot)
}
Expand All @@ -43,10 +41,7 @@ export class IdentifierType<T> extends Type<T, T> {
}

isValidSnapshot(value: any, context: IContext): IValidationResult {
if (this.identifierType.is(value)) {
return typeCheckSuccess()
}
return typeCheckFailure(context, value)
return this.identifierType.validate(value, context)
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ test("should throw if multiple identifiers provided", t => {
}, `[mobx-state-tree] Cannot define property 'pk' as object identifier, property 'id' is already defined as identifier property`)
})

test("should throw if identifier of wrong type", t => {
t.throws(() => {
const Model = types.model("Model", {
id: types.identifier(types.number)
})

Model.create({ id: "1"})
}, `[mobx-state-tree] Error while converting \`{"id":"1"}\` to \`Model\`:
at path "/id" value \`"1"\` is not assignable to type: \`identifier(number)\`, expected an instance of \`identifier(number)\` or a snapshot like \`identifier(number)\` instead.`)
})

test("identifier should be used only on model types - no parent provided", t => {
t.throws(() => {
const Model = types.identifier(types.number)
Expand Down

0 comments on commit 798f76f

Please sign in to comment.