Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface FieldDecoratorResult<T>

Type parameters

  • T

Hierarchy

  • FieldDecoratorResult

Callable

  • __call(target: Object, property?: undefined | string, parameterIndexOrDescriptor?: any): void
  • Parameters

    • target: Object
    • Optional property: undefined | string
    • Optional parameterIndexOrDescriptor: any

    Returns void

Index

Decorator Example ```typescript export class PageCollection { @f.forward(() => PageClass).decorated() private readonly pages: PageClass[] = []; constructor(pages: PageClass[] = []) { this.pages = pages; } public count(): number { return this.pages.length; } public add(name: string): number { return this.pages.push(new PageClass(name)); } } export class PageClass { @f.uuid() id: string = uuid(); @f name: string; @f.forward(() => PageCollection) children: PageCollection = new PageCollection; constructor(name: string) { this.name = name; } } ``` If you use classToPlain(PageClass, ...) or classToMongo(PageClass, ...) the field value of `children` will be the type of `PageCollection.pages` (always the field where @Decorated() is applied to), here a array of PagesClass `PageClass[]`. Methods

Other Methods

Decorator Example ```typescript export class PageCollection { @f.forward(() => PageClass).decorated() private readonly pages: PageClass[] = []; constructor(pages: PageClass[] = []) { this.pages = pages; } public count(): number { return this.pages.length; } public add(name: string): number { return this.pages.push(new PageClass(name)); } } export class PageClass { @f.uuid() id: string = uuid(); @f name: string; @f.forward(() => PageCollection) children: PageCollection = new PageCollection; constructor(name: string) { this.name = name; } } ``` If you use classToPlain(PageClass, ...) or classToMongo(PageClass, ...) the field value of `children` will be the type of `PageCollection.pages` (always the field where @Decorated() is applied to), here a array of PagesClass `PageClass[]`. Methods

decorated

  • decorated(): this
  • Used to define a field as decorated. This is necessary if you want to wrap a field value in the class instance using a own class, like for example for Array manipulations, but keep the JSON and Database value as primitive as possible.

    Only one field per class can be the decorated one.

    Returns this

Other Methods

asArray

  • asArray(): this
  • Marks a field as array. You should prefer @f.array(T) syntax.

    class User {
        @f.type(String).asArray()
        tags: strings[] = [];
    }

    Returns this

asId

  • asId(): this
  • see

    primary

    deprecated

    Returns this

asMap

  • asMap(): this
  • Marks a field as map. You should prefer @f.map(T) syntax.

    class User {
        @f.type(String).asMap()
        tags: {[name: string]: string} = [];
    }

    Returns this

asName

  • asName(name: string): this
  • Sets the name of this property. Important for cases where the actual name is lost during compilation.

    Parameters

    • name: string

    Returns this

asPartial

  • asPartial(): this
  • internal

    Returns this

backReference

  • Marks this reference as not-owning side.

    options.via: If the foreign class is not directly accessible, you can use a pivot collection/table using the via option. Make sure that the given class in via contains both reference (one back to this class and one to the actual foreign class).

    options.mappedBy: Explicitly set the name of the @f.reference() of the foreign class to which this backReference belongs to. Per default it is automatically detected, but will fail if you the foreign class contains more than one @f.reference() to this class.

    Parameters

    Returns this

discriminant

  • discriminant(): this
  • Marks this field as discriminant for the discriminator in union types. See @f.union()

    Returns this

exclude

  • exclude(t?: "all" | "database" | "plain" | string): this
  • Used to define a field as excluded when serialized from class to different targets (like mongo or plain). PlainToClass or mongoToClass is not effected by this. This exclusion is during compile time, if you need a runtime exclude/include mechanism, please use @f.group('groupName') and use in classToPain/partialClassToPlain the options argument to filter, e.g. {groupsExclude: ['groupName']}.

    Parameters

    • Optional t: "all" | "database" | "plain" | string

    Returns this

group

  • group(...names: string[]): this
  • Puts the property into one or multiple groups. Groups can be used to serialize only a subset of properties. It's recommended to use enums to make sure you don't have magic untyped strings.

    enum Group {
        confidential = 'confidential';
    }
    
    class User {
        @f username: string;
        @f.group(Group.confidential) password: string;
    
        @f.group('bar') foo: string;
    }
    
    const user = new User();
    
    const plain = partialClassToPlain(User, user, {groupsExclude: [Group.confidential]});

    Parameters

    • Rest ...names: string[]

    Returns this

index

  • index(options?: IndexOptions, name?: undefined | string): this
  • Used to define an index on a field.

    Parameters

    • Optional options: IndexOptions
    • Optional name: undefined | string

    Returns this

mongoId

  • mongoId(): this
  • Used to define a field as MongoDB ObjectId. This decorator is necessary if you want to use Mongo's _id.

    class Page {
        @f.mongoId()
        referenceToSomething?: string;
    
        constructor(
            @f.id().mongoId()
            public readonly _id: string
        ) {
    
        }
    }

    Returns this

optional

  • optional(): this
  • Marks this field as optional. The validation requires field values per default, this makes it optional.

    Returns this

primary

  • primary(): this
  • Marks this field as an ID aka primary. This is important if you interact with the database abstraction.

    Only one field in a class can be the ID.

    Returns this

reference

  • reference(): this
  • Marks this field as owning reference to the foreign class.

    Its actual value is not written into the document itself, but stored in its own collection/table and a reference is established using its primary field. Without reference() field values are embedded into the main document.

    Owning reference means: Additional foreign key fields are automatically added if not already explicitly done. Those additional fields are used to store the primary key of the foreign class.

    Returns this

template

  • template(...templateArgs: any[]): this
  • Defines template arguments of a tempalted class. Very handy for types like Observables.

    class Stuff {
    }
    
    class Page {
        @f.t(Stuff)
        downloadStuff(): Observable<Stuff> {
             return new Observable<Stuff>((observer) => {
                 observer.next(new Stuff());
             })
        }
    
        //or more verbose way if the type is more complex.
        @f.t(f.type(Stuff).optional())
        downloadStuffWrapper(): Observable<Stuff | undefined> {
             return new Observable<Stuff>((observer) => {
                 observer.next(new Stuff());
             })
        }
    }

    Parameters

    • Rest ...templateArgs: any[]

    Returns this

use

  • use(decorator: (target: Object, property: PropertySchema) => void): this
  • Uses an additional modifier to change the PropertySchema.

    Parameters

    Returns this

uuid

  • uuid(): this
  • Used to define a field as UUID (v4).

    Returns this

validator

  • Adds a custom validator class or validator callback.

    example
    import {PropertyValidator, PropertyValidatorError} from '@marcj/marshal';
    
    class MyCustomValidator implements PropertyValidator {
         async validate<T>(value: any, target: ClassType<T>, propertyName: string): PropertyValidatorError | void {
             if (value.length > 10) {
                 return new PropertyValidatorError('too_long', 'Too long :()');
             }
         };
    }
    
    class Entity {
        @f.validator(MyCustomValidator)
        name: string;
    
        @f.validator(MyCustomValidator)
        name: string;
    
        @f.validator((value: any, target: ClassType<any>, propertyName: string) => {
             if (value.length > 255) {
                 return new PropertyValidatorError('too_long', 'Too long :()');
             }
        })
        title: string;
    }
    

    Parameters

    Returns this

Generated using TypeDoc