1

I have the following JSON:

validate = {
    '(\\.org|\\.info|\\.biz|\\.name)$': [
        { 'type': 'size', 'pattern': /^.{3,64}$/, 'error': 'Your domain can have at max 26 characters and at least 3.' }
    ],
    '.*': [
        { 'type': 'general', 'pattern': /^[^\.-].*[^\.-]$/, 'message': 'Your domain name shouldn\'t contain . or - at the beginning or the end.' },
        { 'type': 'characters', 'pattern': /^[abcdefghijklmnopqrstwuvxyz0123456789]+$/, 'error': 'Your domain can have at max 26 characters and at least 3.' }
    ]
};

and tried to use like this:

var validate = new Dictionary<string, dynamic> {
    {
        @"(\.org|\.info|\.biz|\.name)$",
        new {
            Type = "size",
            Pattern = @"^.{3,64}$",
            Message = "Your domain can have at max 26 characters and at least 3."
        }
    }
};

Where the key of the dynamic object its the regex pattern for the domain extension and the regex inside Pattern key its the one that should match the domain name.

But I can't figure out how I put 2 validations type inside the dynamic part of the Dictionary.

Has anyone did anything like it before or it's stupid and I should do in another way?

The point of doing it this way it's that I can serialize the dictionary as a Json.

Fábio Batista
  • 25,002
  • 3
  • 56
  • 68
Guilherme David da Costa
  • 2,318
  • 4
  • 32
  • 46
  • 2
    what are you trying to accomplish? – Jason Oct 31 '11 at 17:22
  • I'm trying to create a custom data annotation with it and afterwards I wanna send that content from the dictionary as a Json to the view using asp-net-mvc3 so I can have the validation rules on the C# code and everytime we change the C# validation, the client side will follow. – Guilherme David da Costa Oct 31 '11 at 17:24

2 Answers2

1

if you are trying to do this for an MVC site I think you should look into custom validation, and let the runtime handle the plumbing for you. This question which discusses how to implement a custom RegularExpressionAttribute, which seems here.

Community
  • 1
  • 1
Jason
  • 15,915
  • 3
  • 48
  • 72
  • I'm pretty sharp at custom validation, specially if it is mvc3, regardless your link its from mvc2 I don't think they have too much diference. [This](http://stackoverflow.com/q/2383669/840901) question can't solve my problem when it comes to send that regex validation as a serialized Json to the view. – Guilherme David da Costa Oct 31 '11 at 17:45
  • It looks like you are trying to validate a regex, no? – Jason Oct 31 '11 at 17:48
  • I'm trying to validate using regex, but I need inside a dictionary because I want to iterate it after. – Guilherme David da Costa Oct 31 '11 at 17:57
1

I tried with a List<dynamic> at the dynamic part of my Dictionary:

var validate = new Dictionary<string, List<dynamic>> {
    {
        "(\\.org|\\.info|\\.biz|\\.name)$",
        new List<dynamic>
        {
            new {
                Type = "size",
                Pattern = @"^.{3,64}$",
                Message = "Your domain can have at max 26 characters and at least 3."
            }
        }
    },
    {
        ".*",
        new List<dynamic>
        {
            new {
                Type = "general",
                Pattern = @"^[^\.-].*[^\.-]$",
                Message = "Your domain name shouldn\'t contain . or - at the beginning or the end."
            },
            new {
                Type = "characters",
                Pattern = @"^[abcdefghijklmnopqrstwuvxyz0123456789]+$",
                Message = "Your domain name should contain only alphanumeric characters."
            }
        }
    }
};

And using Json from JsonResult mvc3 view it returned the json I needed.

Fábio Batista
  • 25,002
  • 3
  • 56
  • 68
Guilherme David da Costa
  • 2,318
  • 4
  • 32
  • 46