diff --git a/src/types/utility-types/identifier.ts b/src/types/utility-types/identifier.ts index 78b2e4b0d..64bf56fb3 100644 --- a/src/types/utility-types/identifier.ts +++ b/src/types/utility-types/identifier.ts @@ -26,8 +26,6 @@ export class IdentifierType extends Type { 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) } @@ -43,10 +41,7 @@ export class IdentifierType extends Type { } isValidSnapshot(value: any, context: IContext): IValidationResult { - if (this.identifierType.is(value)) { - return typeCheckSuccess() - } - return typeCheckFailure(context, value) + return this.identifierType.validate(value, context) } } diff --git a/test/identifier.ts b/test/identifier.ts index 2735a10a1..350d1a5ce 100644 --- a/test/identifier.ts +++ b/test/identifier.ts @@ -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)