blob: 15c2e237e3f0e127e8ac154691236b8bfca04987 [file] [log] [blame]
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +01001// Copyright 2018 The CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cue
16
17// validate returns whether there is any error, recursively.
18func validate(ctx *context, v value) *bottom {
19 eval := v.evalPartial(ctx)
Marcel van Lohuizen94d845d2019-05-10 00:28:03 +020020 if err, ok := eval.(*bottom); ok && err.code != codeIncomplete && err.code != codeCycle {
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +010021 return eval.(*bottom)
22 }
23 switch x := eval.(type) {
24 case *structLit:
Marcel van Lohuizen7f48df72019-02-01 17:24:59 +010025 x = x.expandFields(ctx)
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +010026 for i := range x.arcs {
27 if err := validate(ctx, x.at(ctx, i)); err != nil {
28 return err
29 }
30 }
31 case *list:
Marcel van Lohuizen9ee652d2019-04-25 17:16:01 +020032 // TODO: also validate types for open lists?
33 return validate(ctx, x.elem)
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +010034 }
35 return nil
36}