How to use the jsonargparse.ActionJsonSchema function in jsonargparse

To help you get started, we’ve selected a few jsonargparse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github omni-us / jsonargparse / View on Github external
def _extend_jsonvalidator_with_default(validator_class):
        """Extends a json schema validator so that it fills in default values."""
        validate_properties = validator_class.VALIDATORS['properties']

        def set_defaults(validator, properties, instance, schema):
            for property, subschema in properties.items():
                if 'default' in subschema:
                    instance.setdefault(property, subschema['default'])

            for error in validate_properties(validator, properties, instance, schema):
                yield error

        return jsonschema.validators.extend(validator_class, {'properties': set_defaults})

class ActionJsonnetExtVars(ActionJsonSchema):
    """Action to be used for jsonnet ext_vars."""
    def __init__(self, **kwargs):
        super().__init__(schema={'type': 'object'}, with_meta=False)

class ActionJsonnet(Action):
    """Action to parse a jsonnet, optionally validating against a jsonschema."""
    def __init__(self, **kwargs):
        """Initializer for ActionJsonnet instance.

            ext_vars (str or None): Key where to find the external variables required to parse the jsonnet.
            schema (str or object or None): Schema to validate values against. Keyword argument required even if schema=None.

            ValueError: If a parameter is invalid.
github omni-us / jsonargparse / View on Github external
def __call__(self, *args, **kwargs):
        """Parses an argument validating against the corresponding jsonschema.

            TypeError: If the argument is not valid.
        if len(args) == 0:
            kwargs['_validator'] = self._validator
            kwargs['_with_meta'] = self._with_meta
            if 'help' in kwargs and '%s' in kwargs['help']:
                kwargs['help'] = kwargs['help'] % json.dumps(self._validator.schema, indent=2, sort_keys=True)
            return ActionJsonSchema(**kwargs)
        val = self._check_type(args[2])
        if not self._with_meta:
            val = strip_meta(val)
        setattr(args[1], self.dest, val)