| # Copyright 2018 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import("//build/compiled_action.gni") |
| |
| # Validate a JSON file against a JSON schema. |
| # |
| # data (required) |
| # [file] JSON file to validate. |
| # If this file is not valid JSON, or does not meet the schema, then this |
| # target will fail to build. |
| # |
| # schema (required) |
| # [file] Schema to use for validation. |
| # |
| # sources (optional) |
| # [list of files] Additional schema files referenced by schema. |
| # Additional schema files used by the original schema file must be listed |
| # here for validation to be re-run when they change. |
| # |
| # allow_comments (optional; default is false) |
| # [bool] If true, the |data| file may contain C-style comments |
| # (`/* block */` and `// inline`) that will be ignored. |
| # |
| # deps (optional) |
| # public_deps (optional) |
| # testonly (optional) |
| # visibility (optional) |
| # Standard GN meaning. |
| # |
| # Example of usage: |
| # |
| # validate_json("validate_my_json") { |
| # data = "my_file.json" |
| # schema = "my_schema.json" |
| # } |
| template("validate_json") { |
| assert(defined(invoker.data), "data is required") |
| assert(defined(invoker.schema), "schema is required") |
| allow_comments = defined(invoker.allow_comments) && invoker.allow_comments |
| |
| compiled_action(target_name) { |
| forward_variables_from(invoker, |
| [ |
| "deps", |
| "sources", |
| "public_deps", |
| "testonly", |
| "visibility", |
| ]) |
| |
| tool = "//build/tools/json_validator" |
| |
| stamp_file = "$target_gen_dir/$target_name.verified" |
| |
| inputs = [ |
| invoker.data, |
| invoker.schema, |
| ] |
| |
| outputs = [ stamp_file ] |
| |
| args = [] |
| |
| if (allow_comments) { |
| args += [ "--allow_comments" ] |
| } |
| |
| args += [ |
| rebase_path(invoker.schema), |
| rebase_path(invoker.data), |
| rebase_path(stamp_file), |
| ] |
| } |
| } |