# Field Definitions Field definitions in JXT are the entries in the `fields` section of a definition: ```ts registry.define({ namespace: 'test', element: 'x', path: 'example', fields: { // Field definitions are here } }); ``` Each field can include an `importer()` and `exporter()` function, where the `importer()` function is responsible for converting an XML object into JSON, and `exporter()` is used to convert JSON to XML: ```ts type FieldImporter = (xml: XMLElement, context: TranslationContext) => any; type FieldExporter = (xml: XMLElement, data: any, context: TranslationContext) => void; interface FieldDefinition { importer: FieldImporter; exporter: FieldExporter; } ``` The `importer()` is given the source XML element, and is expected to return whatever value should be assigned to the field name. The `exporter()` object is given the XML element that is being expanded, the current value of the field. The `exporter()` does not return anything, it is expected to modify the given XML element in-place. The `TranslationContext` can be used to access information such as the languages to accept, the current key path, the JXT registry being used, etc. ## Custom Field Definitions In nearly all cases, you will use a function to create field definitions dynamically. Here is an example implementation of a field that gets/sets data from an attribute. ```ts const attributeField = (attrName: string): FieldDefinition => ({ importer(xml: XMLElement) { return xml.getAttribute(attrName); }, exporter(xml: XMLElement, value: string) { xml.setAttribute(attrName, value); } }); ``` This `attributeField()` function can now be used in a JXT definition: ```ts registry.define({ namespace: 'test', element: 'x', path: 'example', fields: { attribute: attributeField('someAttrName') } }); ``` ## Provided Field Definition Types JXT comes bundled with a variety of pre-defined field definitions. These definitions come in several flavors: - `[type]Attribute()` Map and cast an attribute value to the given type (boolean, integer, etc). - `namespaced[Type]Attribute` Same as `[type]Attribute`, but the attribute also uses an XML namespace. - `child[Type]Attribute` The same as `[type]Attribute`, but the attribute is on a child XML element. - `child[Type]` Map a value from a child XML element, and cast to the given type. - `multipleChild[Type]` Similar to `child[Type]`, but maps an array of values. - `childLanguage[Type]` Similar to `child[Type]`, but the child element that is used is based on the current language. The language used when exporting can be found in the `lang` property of the `TranslationContext`, and the set of languages to consider when importing can be found in the `acceptLanguages` property. - `childAlternateLanguage[Type]` Alternate language definitions use a dictionary mapping language codes to desired values. This definition kind should be used along side `childLanguage[Type]` to capture all available language-based values in addition to the "default" provided by `childLanguage[Type]`. Some of these definitions can accept a `defaultValue` parameter. When a default value is given, it is **only** applied when importing from XML to JSON. The default value will not be applied to any exported XML. --- ### Attribute ```ts attribute(name: string, defaultValue?: string, emitEmpty?: boolean) ``` | Parameter | Type | Description | | ------------ | ------------------------ | ------------------------------------------------------------------------------------ | | name | `string` | The name of the attribute | | defaultValue | `string` \| `undefined` | Default value to use if the attribute is not present, or is empty. | | emitEmpty | `boolean` \| `undefined` | In some cases, the attribute value `""` instead of eliding the attribute is desired. | **Returns:** `string` [See examples](../../test/jxt/examples/attribute.ts) --- ### Boolean Attribute ```ts booleanAttribute(name: string) ``` | Parameter | Type | Description | | --------- | -------- | ------------------------- | | name | `string` | The name of the attribute | **Returns:** `boolean` [See examples](../../test/jxt/examples/booleanAttribute.ts) --- ### Child Alternate Language RawElement ```ts childAlternateLanguageRawElement(namespace: string | null, element: string, sanitizer?: string) ``` | Parameter | Type | Description | | --------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | namespace | `string` \| `null` | The namespace of the child element. If `null`, the parent namespace will be used. | | element | `string` | The name of the child element | | sanitizer | `string` | The name of a sanitizer function to remove any potentially harmful elements or attribute values (e.g. `