blob: 5cdc522114dba2c2f4f44833495e4a2f1b4f976f [file] [log] [blame]
# 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.
#
# use_valico (optional; default is false)
# [bool] If true, a valico-based validator will be used instead of the
# previous rapidjson-based version.
#
# 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
use_valico = defined(invoker.use_valico) && invoker.use_valico
compiled_action(target_name) {
forward_variables_from(invoker,
[
"deps",
"sources",
"public_deps",
"testonly",
"visibility",
])
if (use_valico) {
tool = "//build/tools/json_validator:json_validator_valico"
} else {
tool = "//build/tools/json_validator"
}
stamp_file = "$target_gen_dir/$target_name.json_validated"
inputs = [
invoker.data,
invoker.schema,
]
outputs = [ stamp_file ]
args = []
if (allow_comments) {
if (use_valico) {
args += [ "--json5" ]
} else {
args += [ "--allow_comments" ]
}
}
args += [
rebase_path(invoker.schema, root_build_dir),
rebase_path(invoker.data, root_build_dir),
rebase_path(stamp_file, root_build_dir),
]
}
}
# Validate a JSON5 file against a JSON schema.
#
# data (required)
# [file] JSON5 file to validate.
# If this file is not valid JSON5, 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.
#
# deps (optional)
# public_deps (optional)
# testonly (optional)
# visibility (optional)
# Standard GN meaning.
#
# Example of usage:
#
# validate_json5("validate_my_json") {
# data = "my_file.json5"
# schema = "my_schema.json"
# }
template("validate_json5") {
assert(defined(invoker.data), "data is required")
assert(defined(invoker.schema), "schema is required")
compiled_action(target_name) {
forward_variables_from(invoker,
[
"deps",
"sources",
"public_deps",
"testonly",
"visibility",
])
tool = "//build/tools/json_validator:json_validator_valico"
stamp_file = "$target_gen_dir/$target_name.json5_validated"
inputs = [
invoker.data,
invoker.schema,
]
outputs = [ stamp_file ]
args = [
"--json5",
rebase_path(invoker.schema, root_build_dir),
rebase_path(invoker.data, root_build_dir),
rebase_path(stamp_file, root_build_dir),
]
}
}