Decorators for generating xsd schemas
npm install xsd-decorators
npm install xsd-decorators --save
`Usage
$3
`typescript
import {XSDComplexType, XSDElement} from "xsd-decorators";@XSDComplexType({
choices: {
[PurchaseOrder.PaymentChoice]: {
minOccurs: 1,
maxOccurs: 1
}
}
})
export class PurchaseOrder {
private static readonly PaymentChoice = 'payment-choice';
@XSDAttribute
dateTime: Date;
@XSDElement({
minOccurs: 1,
maxOccurs: 1,
})
shipTo: Customer;
@XSDElement({
minOccurs: 1,
maxOccurs: 1,
})
billTo: Customer;
@XSDElement({
enumeration: ['same-day', 'express', 'lazy']
})
delivery: string;
@XSDElement({
choiceName: PurchaseOrder.PaymentChoice
})
payPalPayment: PayPalPayment;
@XSDElement({
choiceName: PurchaseOrder.PaymentChoice
})
creditCardPayment: CreditCardPayment;
@XSDElement({
maxOccurs: 10,
maxLength: 250
})
comment: string;
@XSDElement
items: Items;
}
`
(Click here for full example )$3
`typescript
import {createSchemaXml} from "xsd-decorators";const xml = createSchemaXml({
elementName: 'purchaseOrder',
target: PurchaseOrder,
targetNamespace: 'http://purchase.example.com',
namespaces: {
wsdl: 'http://schemas.xmlsoap.org/wsdl/'
}
});
`
Result
`xml
`
xsd-decorators uses xml-decorators,
which uses js2xmlparser, for serialization.
So if you want to retrieve the js2xmlparser schema call createJsonSchema with the same
options.Documentation
$3
| name | type | description |
|----------|---------------------------------|-----------------------------------------------------------------------------|
| name? | string | Alternative name of complex type. Overrides inferred name of class. |
| suffix? | string | Adds a suffix to the name of complex type. |
| prefix? | string | Adds a prefix to the name of complex type. |
| choices? | {[name: string]: } | Key/value pairs, for defining choice options for specified key/choice name. |$3
| name | type | description |
|-----------------|------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
| type? | xsd primitive type|Class type | The type of the xsd:element. Normally the type value will be inferred from the type annotation, but this is not always possible. |
| simpleTypeName? | string | Overrides inferred simple type name. |
| choiceName? | string | Identifies to which choice the annotated element belongs. |
| minOccurs? | number | see (w3: declare an element)[https://www.w3.org/TR/xmlschema11-1/#declare-element] |
| maxOccurs? | number | see (w3: declare an element)[https://www.w3.org/TR/xmlschema11-1/#declare-element] |
| minLength? | number | Creates a xsd:pattern to restrict the length of the elements value to the specified value. (TODO: use native minLength instead) |
| maxLength? | number | Creates a xsd:pattern to restrict the length of the elements value to the specified value. (TODO: use native minLength instead) |
| enumeration? | Array | A list of valid values for the annotated element. |
| pattern? | RegExp | Restricts the value of the annotated element by a specified regular expression. |
| attributes? | {[attrName: string]: } | Defines attributes for the xsd:element. (Only available for primitive types or primitive arrays - For complex types, define attributes with the attribute annotation in the corresponding class) |$3
| name | type | description |
|-----------------|------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------|
| type? | xsd primitive type|Class type | The type of the xsd:element. Normally the type value will be inferred from the type annotation, but this is not always possible. |
| simpleTypeName? | string | Overrides inferred simple type name. |
| minLength? | number | Creates a xsd:pattern to restrict the length of the elements value to the specified value. (TODO: use native minLength instead) |
| maxLength? | number | Creates a xsd:pattern to restrict the length of the elements value to the specified value. (TODO: use native minLength instead) |
| enumeration? | Array | A list of valid values for the annotated element. |
| pattern? | RegExp | Restricts the value of the annotated element by a specified regular expression. |$3
The following javascript types can automatically inferred to a xsd type| js type | xsd primitive type |
|-----------------|---------------------|
| String | xsd:string |
| Number | xsd:int |
| Date | xsd:dateTime |
| Boolean | xsd:boolean |
When passing a class/constructor function with
@XSDComplexType` annotation, - [ ] xsd:annotation
- [x] xsd:attribute
- [x] xsd:choice (implicit through decorator options)
- [x] xsd:complexType
- [x] xsd:element
- [ ] xsd:group
- [ ] xsd:import
- [ ] xsd:include
- [x] xsd:restrictions (implicit through decorator options)
- [x] xsd:enumeration
- [ ] xsd:fractionDigits
- [ ] xsd:length
- [ ] xsd:maxExclusive
- [ ] xsd:maxInclusive
- [x] xsd:maxLength
- [ ] xsd:minExclusive
- [ ] xsd:minInclusive
- [x] xsd:minLength
- [x] xsd:pattern
- [ ] xsd:totalDigits
- [ ] xsd:whiteSpace
- [x] xsd:simpleType (implicit through decorator options)
- [ ] ...