-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathValidateLibrary.java
More file actions
73 lines (66 loc) · 2.72 KB
/
ValidateLibrary.java
File metadata and controls
73 lines (66 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2023-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package build.buf.protovalidate;
import dev.cel.checker.CelCheckerBuilder;
import dev.cel.common.CelVarDecl;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompilerLibrary;
import dev.cel.parser.CelParserBuilder;
import dev.cel.parser.CelStandardMacro;
import dev.cel.runtime.CelRuntimeBuilder;
import dev.cel.runtime.CelRuntimeLibrary;
import dev.cel.runtime.CelStandardFunctions;
import dev.cel.runtime.CelStandardFunctions.StandardFunction;
import dev.cel.runtime.CelStandardFunctions.StandardFunction.Overload.Conversions;
/**
* Custom {@link CelCompilerLibrary} and {@link CelRuntimeLibrary}. Provides all the custom
* extension function definitions and overloads.
*/
final class ValidateLibrary implements CelCompilerLibrary, CelRuntimeLibrary {
/** Creates a ValidateLibrary with all custom declarations and overloads. */
ValidateLibrary() {}
@Override
public void setParserOptions(CelParserBuilder parserBuilder) {
parserBuilder.setStandardMacros(
CelStandardMacro.ALL,
CelStandardMacro.EXISTS,
CelStandardMacro.EXISTS_ONE,
CelStandardMacro.FILTER,
CelStandardMacro.HAS,
CelStandardMacro.MAP,
CelStandardMacro.MAP_FILTER);
}
@Override
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
checkerBuilder
.addVarDeclarations(
CelVarDecl.newVarDeclaration(NowVariable.NOW_NAME, SimpleType.TIMESTAMP))
.addFunctionDeclarations(CustomDeclarations.create());
}
@Override
public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
runtimeBuilder
.addFunctionBindings(CustomOverload.create())
.setStandardEnvironmentEnabled(false)
.setStandardFunctions(
CelStandardFunctions.newBuilder()
.filterFunctions(
// CEL doesn't validate, that the bytes are valid utf-8, so we provide our own
// implementation.
(function, overload) ->
function != StandardFunction.STRING
|| !overload.equals(Conversions.BYTES_TO_STRING))
.build());
}
}