Skip to content

Instantly share code, notes, and snippets.

@kasper573
Last active November 10, 2018 21:10
Show Gist options
  • Save kasper573/03e36681a5b3fde50e94e704baf5bdde to your computer and use it in GitHub Desktop.
Save kasper573/03e36681a5b3fde50e94e704baf5bdde to your computer and use it in GitHub Desktop.
import {getDefaultModelSchema, object, PropSchema} from 'serializr';
/**
* A custom serializr schema that behaves like object() but requires the model later, allowing circular references.
*/
export function fnObject<T> (getModel: () => new (...args: any[]) => T): PropSchema {
const getSchema = () => getDefaultModelSchema(getModel());
return {
serializer: (...args: any[]) => (object(getSchema()).serializer as any)(...args),
deserializer: (...args: any[]) => (object(getModel()).deserializer as any)(...args)
};
}
@kasper573
Copy link
Author

@serializable(alias('mediakits', onlyIn(list(fnObject(() => require('./MediaKit').AnonymousMediaKit)))))
mediaKits: MediaKit[] = [];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment