Trending

World technology news

→
An interoperable job manifest with the 2019 CDDL standard
Software Engineering · 2019
Software Engineering2019

An interoperable job manifest with the 2019 CDDL standard

Use a compact CDDL contract for a batch of jobs, test the accepted JSON shape, and choose how the contract can evolve before adding a CBOR transport.

Archive retrospective · Topic year 2019

Developer's JournalPublished: Dec 4, 20266 min read
Reading progress0%
In this article
  1. Start with two producers that disagree
  2. Draw the distinction between a map and an array
  3. Give missing and malformed values distinct meanings
  4. Read the fixture output alongside the declaration
  5. Roll out an extension where the reader can see it
  6. Retain decisions that a schema cannot express here

Start with two producers that disagree

An office uploads a print-and-dispatch manifest as JSON. A handheld terminal wants to transmit the same kind of batch as CBOR. If an example payload is the only contract, the implementations can diverge while both still look reasonable. One may accept a fractional copy count and round it, while the other sends the batch to an error queue. The June 2019 CDDL specification, RFC 8610, provides a notation for describing the data items that appear in both encodings. Keep that description close to the handoff between the producers and the dispatcher. For this example the work is deliberately narrow: a batch needs a revision, at least one job and optionally a human note. Each job has a text identifier, a known operation and a bounded integer for its copy count. A reviewer can discuss which messages belong in this interface before choosing libraries or an encoding.

Draw the distinction between a map and an array

The manifest and each job are maps. The textual keys carry the meaning of their values, so the dispatcher should accept the same map if a serializer moves a key to the end. The job list is an array: its entries have positions. A downstream scheduling choice may let consumers ignore the order, but the contract still describes a list with ordered elements. CDDL groups can describe both structures, with a different matching rule in a map than in an array. The keys and values in this example deliberately sit in the intersection of the two data formats. CBOR can represent kinds of values that plain JSON cannot directly carry. Adding a byte string or a nontext map key therefore introduces a design question for the JSON producer. Keep the representation decision in the interface specification, where the owner of either transport can review it.

Application flowConceptual sketch · summary of the article's technical flow
Illustrative diagram by Yazılımcının Günlüğü

Give missing and malformed values distinct meanings

Use the range one through twenty for copies. Both endpoints belong to the permitted set. A JSON string containing digits is a different value from an integer, so requiring producers to send a number prevents a series of ad hoc conversions. The list cannot be empty. A UI submitting no selected jobs should explain that condition while the dispatcher continues to reject the malformed batch. An optional note can be left out entirely. When a note is supplied it has to be text. This still allows an empty string and excludes null. If operators want to distinguish an explicit request to clear a note, add a model for that operation instead of leaving each client to guess its meaning. Document decisions about trimming and maximum lengths at the appropriate boundary, with a test for any stricter application rule.

Read the fixture output alongside the declaration

The local harness parses the samples as JSON and checks the narrow schema with a handwritten predicate. On Windows with Node.js 24.19.0, the normal batch and reordered map both passed. Five other cases failed as expected: an empty list, a fractional count, an unrecognized key, a different revision, and a null note. Assertions tie each decision to the fixture so a mistaken relaxation cannot disappear inside a successful demo. The declaration printed above the results documents the intended contract. The harness and the declaration are maintained separately, which makes the harness useful as a reference fixture set. For a production validator, run the same cases against the library that actually parses CDDL. No 2019 implementation was exercised here. A CBOR implementation should also get transport-specific fixtures before the example is used as an interoperability test.

import assert from 'node:assert/strict';

const schema = `manifest = {rev: 1, jobs: [1* job], ? note: tstr}
job = {id: tstr, kind: "print" / "ship", copies: 1..20}`;

function checkKeys(value, required, optional = []) {
  return value !== null && typeof value === "object" &&
    !Array.isArray(value) && required.every(key => Object.hasOwn(value, key)) &&
    Object.keys(value).every(key => required.includes(key) || optional.includes(key));
}
function accepted(input) {
  return checkKeys(input, ["rev", "jobs"], ["note"]) && input.rev === 1 &&
    (!Object.hasOwn(input, "note") || typeof input.note === "string") &&
    Array.isArray(input.jobs) && input.jobs.length > 0 &&
    input.jobs.every(job =>
      checkKeys(job, ["id", "kind", "copies"]) && typeof job.id === "string" &&
      ["print", "ship"].includes(job.kind) && Number.isInteger(job.copies) &&
      job.copies >= 1 && job.copies <= 20);
}
const job = {id: "A-17", kind: "print", copies: 2};
const fixtures = [
  ["ordinary", {rev: 1, jobs: [job]}, true],
  ["key order", {jobs: [job], note: "night batch", rev: 1}, true],
  ["empty batch", {rev: 1, jobs: []}, false],
  ["fraction", {rev: 1, jobs: [{...job, copies: 1.5}]}, false],
  ["new key", {rev: 1, jobs: [job], priority: "high"}, false],
  ["new revision", {rev: 2, jobs: [job]}, false],
  ["null note", {rev: 1, jobs: [job], note: null}, false]
];
console.log(schema);
for (const [name, value, expected] of fixtures) {
  const actual = accepted(JSON.parse(JSON.stringify(value)));
  assert.equal(actual, expected, name);
  console.log(name + ": " + (actual ? "accepted" : "rejected"));
}

Roll out an extension where the reader can see it

Suppose dispatchers want a priority field. The original map definition gives that field nowhere to match, so a sender that emits it may break earlier consumers. First establish who still reads revision one. For a versioned rollout, deploy readers that can accept the old and new shapes, monitor actual use, and then enable the revised producers. Alternatively, give future extensions an explicitly scoped map in the first contract. Decide whose keys it may contain as well as which value types and sizes are safe. The strict part of the map helps catch errors such as a misspelled count. An open extension point serves a different purpose. Keep those two review decisions visible and classify errors for an unknown revision separately from errors for a field that appears in the active revision. Good diagnostics buy time when producer and consumer releases are out of step.

Retain decisions that a schema cannot express here

A valid batch can still ask for the same job twice. Keep replay and identity policy in the dispatcher, with a defined issuer scope and a record of jobs already applied. The state of a job after validation should be visible to the operator. The structural error path and the business rejection path deserve separate fixtures because they lead to different corrective actions. Pay attention to the boundary before validation. Ordinary JSON parsing can discard evidence of duplicate keys before the JavaScript predicate runs. If duplicate members are a concern, exercise the parser you deploy with raw fixtures, not just a constructed object. Once the terminal adds CBOR, compare the decoded business values and test the encoding itself independently. That leaves teams with an interface they can check at the boundary and a second layer they can reason about when jobs are queued, retried or refused.

301 reads ✎ Write a comment

Reader discussion

Discussion prompt

Which data would you verify before applying this development in your field?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.