jburzynski / TypeGen

Single-class-per-file C# to TypeScript generator
MIT License
185 stars 54 forks source link

Anotate typescript models with dataannotations validators and community toolkit mvvm attributes #203

Open CastelloBrancoTecnologia opened 1 week ago

CastelloBrancoTecnologia commented 1 week ago

Is there possible ? If not can you help me to implement it ?

Add typescripts decorators to generated typescript. Also support for private class fields.

using System.ComponentModel.DataAnnotations;
using CommunityToolkit.Mvvm;
using CommunityToolkit.Mvvm.ComponentModel;

[ExportTsClass]
public partial class ProductDto : ObservableValidator
{
    [ObservableProperty]
    [Required (ErrorMessage="Price is required")]
    [Range(1, 999, ErrorMessage="price must be in the range [1, 999].")]
    private decimal _price;

    [ObservableProperty]
    private string[] _tags;
}

generated class

export class ProductDto extends ObservableValidator
{
    @Required ("Price is required")
    @Range(1, 999, "price must be in the range [1, 999].")
    private _price: number;

    private _tags: string[];
}

Only genarate c# attributes that inherits from ValidatorAttribute to typescript decorators. All others attributes will be ignored. ErrorMessage attribute property will be translated to msg typescript decorator parameter

jburzynski commented 5 days ago

To generate private fields/properties, the TypeExtensions.WithMembersFilter() method would have to be changed - it filters out non-public members.

Adding custom decorators is currently not supported and would require a more major change in the code.