Subject: Re: [netmod] tool to validate json again yang

From: Ladislav Lhotka <[email-masked]>
Date: Mon, 13 Apr 2015 08:54:50 +0200

Hi Lisa,

"Lisa (Yi) Huang" <[email-masked]> writes:

> Lada and all,
>
> I am looking for a tool validate json instance to yang. Do you have any
> recommendation?
>
> Currently, pyang can generate jtox which I could use to convert json to
> xml. After that the xml can be validated with xsd generated from yang. I
> am wondering whether there is a better way to do it. Thanks,

Yes, there is - use DSDL schemas rather than XSD. The "xsd" plugin in
pyang is deprecated.

Below is a Makefile from a project where we use this way of JSON
validation (make validate). It is still indirect in that it goes via
XML, but I am not aware of any open source tool that can validate JSON
directly.

For more details about XML instance validation using DSDL, see this
tutorial:

https://github.com/mbj4668/pyang/wiki/InstanceValidation

Hope this helps,

Lada

MODULES = securris
ymod = $(addsuffix .yang, $(MODULES))
TARGET ?= config
BASE ?= sample
xsltdir = ../../xslt
bn = $(BASE)-$(TARGET)
schemas = $(bn).rng $(BASE)-gdefs-config.rng $(bn)-dsrl.xsl $(bn)-sch.xsl
tgt = --stringparam target $(TARGET)
rngparms =  $(tgt) --stringparam basename $(BASE) --stringparam schema-dir \
	$(PYANG_RNG_LIBDIR) $(PYANG_XSLT_DIR)/gen-relaxng.xsl
Y2DOPTS = -t $(TARGET) -b $(BASE)
.PHONY = clean

all: $(schemas) model.tree model.xsl

%.yang: %.yinx
	@xsltproc $(xsltdir)/canonicalize.xsl $< | \
	xsltproc --output $@ $(xsltdir)/yin2yang.xsl -

$(bn).xml: model.jtox $(bn).json
	@json2xml -t $(TARGET) -o $@ $^

model.jtox: $(ymod)
	@pyang -o $@ -f jtox $^

model.xsl: $(ymod)
	@pyang -o $@ -f jsonxsl $^

model.tree: $(ymod)
	@pyang -o $@ -f tree $^

$(bn).dsdl: $(ymod)
	@pyang -f dsdl -o $@ --dsdl-no-documentation --dsdl-no-dublin-core $^

$(bn).rng: $(bn).dsdl
	@xsltproc --output $@ $(rngparms) $<

$(BASE)-gdefs-config.rng: $(bn).dsdl
	@xsltproc --output $@ --stringparam gdefs-only 1 $(rngparms) $<

$(bn).rnc: $(bn).rng $(BASE)-gdefs-config.rng
	@trang -I rng -O rnc $< $@

$(bn)-dsrl.xsl: $(bn).dsdl
	@xsltproc $(tgt) $(PYANG_XSLT_DIR)/gen-dsrl.xsl $< | \
	xsltproc --output $@ $(PYANG_XSLT_DIR)/dsrl2xslt.xsl -

$(bn)-sch.xsl: $(bn).dsdl
	@xsltproc $(tgt) $(PYANG_XSLT_DIR)/gen-schematron.xsl $< | \
	xsltproc $(PYANG_XSLT_DIR)/iso_abstract_expand.xsl - | \
        xsltproc -o $@ $(PYANG_XSLT_DIR)/iso_svrl_for_xslt1.xsl -

validate: $(bn).xml $(schemas)
	@jing $(bn).rng $<
	@xsltproc $(bn)-dsrl.xsl $< | xsltproc $(bn)-sch.xsl - | \
	xsltproc $(PYANG_XSLT_DIR)/svrl2text.xsl -

clean:
	@rm -f $(schemas) $(bn).xml *.dsdl model.* *.rnc

> Lisa

-- 
Ladislav Lhotka, CZ.NIC Labs
PGP Key ID: E74E8C0C