Skip to content

Commit 525bb68

Browse files
committed
Added reference field name and field assert.
1 parent 5a5566b commit 525bb68

File tree

6 files changed

+331
-50
lines changed

6 files changed

+331
-50
lines changed

CHANGES.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,34 @@
4444
}
4545
```
4646

47+
- Added reference field name by syntax `$.valueof:field`, e.g.
48+
49+
```json
50+
{
51+
"method": ["=email", "=password"],
52+
"$.valueof:method": "string"
53+
}
54+
```
55+
56+
will `$.valueof:method` means using value of field `method` as the name of
57+
a field. Thus it matches
58+
59+
```json
60+
{
61+
"method": "email",
62+
"email": "aaa@sample.com"
63+
}
64+
```
65+
66+
and
67+
68+
```json
69+
{
70+
"method": "password",
71+
"password": "xxxxxxxxx"
72+
}
73+
```
74+
4775
- Added advanced type `$.dict` to limit keys of `$.map`:
4876

4977
```json

docs/zh-CN/syntax.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ interface Map<T> {
589589

590590
## 进阶使用
591591

592+
### 混合使用
593+
592594
`$.map``$.dict``$.array` 修饰符后面可以有多个元素,例如:
593595

594596
```json
@@ -620,3 +622,37 @@ interface Map<T> {
620622
```
621623

622624
进一步简化为:`"string[]{}"`
625+
626+
### 对象的属性名引用
627+
628+
在对象中,可以通过 `$.valueof` 语法将一个属性的值作为另一个属性的名称,例如:
629+
630+
```json
631+
{
632+
"a": ["=hello", "=world"],
633+
"$.valueof:a": "boolean"
634+
}
635+
```
636+
637+
表示第二个属性的名称必须是属性 a 的值,也就是说,如果 a == "hello",则必须有一个名为
638+
"hello" 的布尔型属性,如果 a == "world",则必须有一个名为 "world" 的布尔型属性。
639+
640+
### 对象的属性存在性断言
641+
642+
在对象中,可以通过 `$.valueof` 语法,将一个属性的值作为另一个属性的名称,并断言该属性
643+
必须存在(不为 `undefined`):
644+
645+
```json
646+
{
647+
"a": ["=hello", "=world"],
648+
"$.valueof:a": "exists",
649+
"$.virtual:hello": "string",
650+
"$.virtual:world": "uint32"
651+
}
652+
```
653+
654+
此处将 `$.valueof:a` 的值设置为 `exists`,表示属性不能为 `undefined`
655+
因此 `hello` 或者 `world` 中必须有一个存在。
656+
657+
> `hello``world` 设置为 `virtual` 属性,这样表示他们被用于存在性断言。
658+
> `virtual` 属性默认是可选的。

sources/common.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export interface CompileResult {
3939

4040
export const BuiltInTypes = {
4141
"void": "void",
42+
"undefined": "undefined",
4243
"optional": "optional",
44+
"exists": "exists",
4345
"array": "array",
4446
"any": "any",
4547
"int": "int",
@@ -65,8 +67,7 @@ export const BuiltInTypes = {
6567
"false": "false",
6668
"null": "null",
6769
"float": "float",
68-
"numeric": "numeric",
69-
"undefined": "undefined"
70+
"numeric": "numeric"
7071
};
7172

7273
function freezeObject(obj: any): void {
@@ -102,17 +103,19 @@ export const FILTER_ON = {
102103
VALUE: "value"
103104
};
104105

105-
export const ADV_TYPE_REL_PREFIX = "$.";
106+
export const ADV_TYPE_PREFIX = "$.";
106107

107108
export const AdvancedTypes = {
108109

109-
$AND: `${ADV_TYPE_REL_PREFIX}and`,
110-
$OR: `${ADV_TYPE_REL_PREFIX}or`,
111-
$TUPLE: `${ADV_TYPE_REL_PREFIX}tuple`,
112-
$ARRAY: `${ADV_TYPE_REL_PREFIX}array`,
113-
$MAP: `${ADV_TYPE_REL_PREFIX}map`,
114-
$DICT: `${ADV_TYPE_REL_PREFIX}dict`,
115-
$STRUCT: `${ADV_TYPE_REL_PREFIX}struct`
110+
$AND: `${ADV_TYPE_PREFIX}and`,
111+
$OR: `${ADV_TYPE_PREFIX}or`,
112+
$TUPLE: `${ADV_TYPE_PREFIX}tuple`,
113+
$ARRAY: `${ADV_TYPE_PREFIX}array`,
114+
$MAP: `${ADV_TYPE_PREFIX}map`,
115+
$DICT: `${ADV_TYPE_PREFIX}dict`,
116+
$STRUCT: `${ADV_TYPE_PREFIX}struct`,
117+
$VALUEOF: `${ADV_TYPE_PREFIX}valueof:`,
118+
$VIRTUAL: `${ADV_TYPE_PREFIX}virtual:`
116119
};
117120

118121
export const IMPLICIT_SYMBOL = "?";
@@ -189,14 +192,13 @@ export interface Language {
189192
getConstantDefinition(constName: string, val: string): string;
190193

191194
/**
192-
* Check if the elements in array variable are insides the array.
193-
*
194-
* @param arrVarName The name of array vairable.
195-
* @param arr The array.
195+
* Check if the keys of object are equal.
196196
*/
197-
getStringArrayContainsCondition(
198-
arrVarName: string,
199-
arr: string[]
197+
getCheckKeysEqualCondition(
198+
objVar: string,
199+
objKeysVar: string,
200+
literalKeys: string[],
201+
referKeys: string[]
200202
): string;
201203

202204
/**

0 commit comments

Comments
 (0)