kazuemon / asn1

TypeScript friendly ASN.1 schema definition, encoding and decoding library
MIT License
1 stars 0 forks source link

✨ Allow Sequence fields to be Optional #2

Closed kazuemon closed 9 months ago

kazuemon commented 9 months ago
const schema = sequence({
  fields: [
    {
      name: "id",
      schema: integer(),
    },
    {
      name: "message",
      schema: octetString(),
      // ✨ New option!
      optional: true,
    },
  ] as const,
});

const asn1Buf = schema.encode({
  // id: number
  id: 50,
  // message?: string | undefined
  message: "hello",
});

const result = schema.decode( /* Asn1Data */ );
const id = result.id; // number
const message = result.message; // string | undefined