diff --git a/shared/src/main/scala/io/kaitai/struct/precompile/TypeValidator.scala b/shared/src/main/scala/io/kaitai/struct/precompile/TypeValidator.scala index d80741484..8457b6837 100644 --- a/shared/src/main/scala/io/kaitai/struct/precompile/TypeValidator.scala +++ b/shared/src/main/scala/io/kaitai/struct/precompile/TypeValidator.scala @@ -82,7 +82,12 @@ class TypeValidator(specs: ClassSpecs) extends PrecompileStep { val problemsDataType = validateDataType(attr.dataType, path) - List(problemsIf, problemsRepeat, problemsDataType).flatten + val problemsValid: Iterable[CompilationProblem] = attr.valid match { + case Some(valid) => validateValidClause(valid, attr.dataType, attr.path :+ "valid") + case None => None // all good + } + + List(problemsIf, problemsRepeat, problemsDataType, problemsValid).flatten } def validateParseInstance(pis: ParseInstanceSpec): Iterable[CompilationProblem] = { @@ -100,7 +105,12 @@ class TypeValidator(specs: ClassSpecs) extends PrecompileStep { case None => None // all good } - List(problemsAttr, problemsIo, problemsPos).flatten + val problemsValid: Iterable[CompilationProblem] = pis.valid match { + case Some(valid) => validateValidClause(valid, pis.dataType, pis.path :+ "valid") + case None => None // all good + } + + List(problemsAttr, problemsIo, problemsPos, problemsValid).flatten } def validateValueInstance(vis: ValueInstanceSpec): Option[CompilationProblem] = { @@ -204,6 +214,42 @@ class TypeValidator(specs: ClassSpecs) extends PrecompileStep { } } + /** + * @param valid The `valid` clause of Kaitai Struct attribute or parse instance definition + * @param attrType The type of attribute, used to check compatibility of valid expressions to that type + * @param path Path where `valid` key is defined + */ + private def validateValidClause( + valid: ValidationSpec, + attrType: DataType, + path: List[String] + ): Iterable[CompilationProblem] = { + // TODO: This is not user-face message + val expected = attrType.toString + valid match { + // TODO: Need to distinguish between `valid`, `valid.eq` and `contents` + case ValidationEq(value) => + checkAssertObject(value, attrType, expected, path, "eq") + case ValidationMin(min) => + checkAssertObject(min, attrType, expected, path, "min") + case ValidationMax(max) => + checkAssertObject(max, attrType, expected, path, "max") + case ValidationRange(min, max) => { + checkAssertObject(min, attrType, expected, path, "min") ++ + checkAssertObject(max, attrType, expected, path, "max") + } + case ValidationAnyOf(values) => { + val itemPath = path :+ "any-of" + values.zipWithIndex.flatMap { case (value, i) => + checkAssertObject(value, attrType, expected, itemPath, i.toString) + } + } + case ValidationInEnum() => List() + case ValidationExpr(checkExpr) => + checkAssert[BooleanType](checkExpr, "boolean", path, "expr") + } + } + /** * Checks that expression's type conforms to a given datatype, otherwise * throw a human-readable exception, with some pointers that would help