1 module jsonschema.standard.constraint;
2 
3 import std, jsonschema;
4 
5 mixin template JsonSchemaIntegerConstraint(string keyword)
6 {
7     static immutable KEYWORD = keyword;
8 
9     size_t value;
10 
11     this(SchemaT.Adapter.AggregateType value)
12     {
13         const valueT = SchemaT.Adapter.getType(value);
14         schemaEnforceType(JsonSchemaType.integer, valueT);
15         this.value = SchemaT.Adapter.getInteger(value);
16     }
17 }
18 
19 mixin template JsonSchemaRegexConstraint(string keyword)
20 {
21     static immutable KEYWORD = keyword;
22 
23     Regex!char value;
24 
25     this(SchemaT.Adapter.AggregateType value)
26     {
27         const valueT = SchemaT.Adapter.getType(value);
28         schemaEnforceType(JsonSchemaType.string_, valueT);
29         this.value = SchemaT.Adapter.getString(value).regex;
30     }
31 }
32 
33 mixin template JsonSchemaArrayConstraint(string keyword, JsonSchemaType schemaType, valueType, string getter)
34 {
35     static immutable KEYWORD = keyword;
36 
37     valueType value;
38 
39     this(SchemaT.Adapter.AggregateType value)
40     {
41         schemaEnforceArrayOf!(SchemaT.Adapter)(schemaType, value);
42         SchemaT.Adapter.eachArrayValue(value, (i,v)
43         {
44             this.value ~= mixin(getter~"(v)");
45         });
46     }
47 }
48 
49 // strings
50 
51 struct JsonSchemaMinLengthConstraint(SchemaT)
52 {
53     mixin JsonSchemaIntegerConstraint!"minLength";
54 }
55 
56 struct JsonSchemaMaxLengthConstraint(SchemaT)
57 {
58     mixin JsonSchemaIntegerConstraint!"maxLength";
59 }
60 
61 struct JsonSchemaPatternConstraint(SchemaT)
62 {
63     mixin JsonSchemaRegexConstraint!"pattern";
64 }
65 
66 // numbers
67 
68 struct JsonSchemaMultipleOfConstraint(SchemaT)
69 {
70     mixin JsonSchemaIntegerConstraint!"multipleOf";
71 }
72 
73 struct JsonSchemaMinimumConstraint(SchemaT)
74 {
75     mixin JsonSchemaIntegerConstraint!"minimum";
76 }
77 
78 struct JsonSchemaMaximumOfConstraint(SchemaT)
79 {
80     mixin JsonSchemaIntegerConstraint!"maximum";
81 }
82 
83 // objects
84 
85 struct JsonSchemaRequiredConstraint(SchemaT)
86 {
87     mixin JsonSchemaArrayConstraint!("required", JsonSchemaType.string_, string, "SchemaT.Adapter.getString");
88 }
89 
90 struct JsonSchemaMinPropertiesConstraint(SchemaT)
91 {
92     mixin JsonSchemaIntegerConstraint!"minProperties";
93 }
94 
95 struct JsonSchemaMaxPropertiesConstraint(SchemaT)
96 {
97     mixin JsonSchemaIntegerConstraint!"maxProperties";
98 }