@grafctory/blue 0.0.0-stage → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +524 -2
- package/dist/boolean/assembler.d.ts +53 -0
- package/dist/boolean/assembler.d.ts.map +1 -0
- package/dist/boolean/assembler.js +78 -0
- package/dist/boolean/assembler.js.map +1 -0
- package/dist/boolean/index.d.ts +86 -0
- package/dist/boolean/index.d.ts.map +1 -0
- package/dist/boolean/index.js +99 -0
- package/dist/boolean/index.js.map +1 -0
- package/dist/boolean/validator.d.ts +30 -0
- package/dist/boolean/validator.d.ts.map +1 -0
- package/dist/boolean/validator.js +68 -0
- package/dist/boolean/validator.js.map +1 -0
- package/dist/dictionary/assembler.d.ts +70 -0
- package/dist/dictionary/assembler.d.ts.map +1 -0
- package/dist/dictionary/assembler.js +178 -0
- package/dist/dictionary/assembler.js.map +1 -0
- package/dist/dictionary/index.d.ts +237 -0
- package/dist/dictionary/index.d.ts.map +1 -0
- package/dist/dictionary/index.js +43 -0
- package/dist/dictionary/index.js.map +1 -0
- package/dist/dictionary/inference.d.ts +42 -0
- package/dist/dictionary/inference.d.ts.map +1 -0
- package/dist/dictionary/inference.js +17 -0
- package/dist/dictionary/inference.js.map +1 -0
- package/dist/dictionary/validator.d.ts +37 -0
- package/dist/dictionary/validator.d.ts.map +1 -0
- package/dist/dictionary/validator.js +106 -0
- package/dist/dictionary/validator.js.map +1 -0
- package/dist/index.core.d.ts +33 -0
- package/dist/index.core.d.ts.map +1 -0
- package/dist/index.core.js +149 -0
- package/dist/index.core.js.map +1 -0
- package/dist/index.d.ts +333 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +378 -0
- package/dist/index.js.map +1 -0
- package/dist/number/assembler.d.ts +70 -0
- package/dist/number/assembler.d.ts.map +1 -0
- package/dist/number/assembler.js +188 -0
- package/dist/number/assembler.js.map +1 -0
- package/dist/number/index.d.ts +329 -0
- package/dist/number/index.d.ts.map +1 -0
- package/dist/number/index.js +312 -0
- package/dist/number/index.js.map +1 -0
- package/dist/number/validator.d.ts +31 -0
- package/dist/number/validator.d.ts.map +1 -0
- package/dist/number/validator.js +57 -0
- package/dist/number/validator.js.map +1 -0
- package/dist/reference/accessors.d.ts +29 -0
- package/dist/reference/accessors.d.ts.map +1 -0
- package/dist/reference/accessors.js +38 -0
- package/dist/reference/accessors.js.map +1 -0
- package/dist/reference/assembler.d.ts +63 -0
- package/dist/reference/assembler.d.ts.map +1 -0
- package/dist/reference/assembler.js +127 -0
- package/dist/reference/assembler.js.map +1 -0
- package/dist/reference/index.d.ts +108 -0
- package/dist/reference/index.d.ts.map +1 -0
- package/dist/reference/index.js +40 -0
- package/dist/reference/index.js.map +1 -0
- package/dist/reference/validator.d.ts +33 -0
- package/dist/reference/validator.d.ts.map +1 -0
- package/dist/reference/validator.js +56 -0
- package/dist/reference/validator.js.map +1 -0
- package/dist/resource/accessors.d.ts +140 -0
- package/dist/resource/accessors.d.ts.map +1 -0
- package/dist/resource/accessors.js +173 -0
- package/dist/resource/accessors.js.map +1 -0
- package/dist/resource/assembler.d.ts +265 -0
- package/dist/resource/assembler.d.ts.map +1 -0
- package/dist/resource/assembler.js +816 -0
- package/dist/resource/assembler.js.map +1 -0
- package/dist/resource/index.d.ts +903 -0
- package/dist/resource/index.d.ts.map +1 -0
- package/dist/resource/index.js +160 -0
- package/dist/resource/index.js.map +1 -0
- package/dist/resource/inference.d.ts +310 -0
- package/dist/resource/inference.d.ts.map +1 -0
- package/dist/resource/inference.js +17 -0
- package/dist/resource/inference.js.map +1 -0
- package/dist/resource/validator.d.ts +158 -0
- package/dist/resource/validator.d.ts.map +1 -0
- package/dist/resource/validator.js +1013 -0
- package/dist/resource/validator.js.map +1 -0
- package/dist/string/assembler.d.ts +69 -0
- package/dist/string/assembler.d.ts.map +1 -0
- package/dist/string/assembler.js +143 -0
- package/dist/string/assembler.js.map +1 -0
- package/dist/string/index.d.ts +431 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +528 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/validator.d.ts +30 -0
- package/dist/string/validator.d.ts.map +1 -0
- package/dist/string/validator.js +69 -0
- package/dist/string/validator.js.map +1 -0
- package/dist/union/accessors.d.ts +137 -0
- package/dist/union/accessors.d.ts.map +1 -0
- package/dist/union/accessors.js +253 -0
- package/dist/union/accessors.js.map +1 -0
- package/dist/union/assembler.d.ts +56 -0
- package/dist/union/assembler.d.ts.map +1 -0
- package/dist/union/assembler.js +140 -0
- package/dist/union/assembler.js.map +1 -0
- package/dist/union/index.d.ts +172 -0
- package/dist/union/index.d.ts.map +1 -0
- package/dist/union/index.js +45 -0
- package/dist/union/index.js.map +1 -0
- package/dist/union/inference.d.ts +30 -0
- package/dist/union/inference.d.ts.map +1 -0
- package/dist/union/inference.js +17 -0
- package/dist/union/inference.js.map +1 -0
- package/dist/union/validator.d.ts +94 -0
- package/dist/union/validator.d.ts.map +1 -0
- package/dist/union/validator.js +171 -0
- package/dist/union/validator.js.map +1 -0
- package/dist/value/accessors.d.ts +151 -0
- package/dist/value/accessors.d.ts.map +1 -0
- package/dist/value/accessors.js +468 -0
- package/dist/value/accessors.js.map +1 -0
- package/dist/value/assembler.d.ts +129 -0
- package/dist/value/assembler.d.ts.map +1 -0
- package/dist/value/assembler.js +248 -0
- package/dist/value/assembler.js.map +1 -0
- package/dist/value/index.d.ts +309 -0
- package/dist/value/index.d.ts.map +1 -0
- package/dist/value/index.js +26 -0
- package/dist/value/index.js.map +1 -0
- package/dist/value/inference.d.ts +292 -0
- package/dist/value/inference.d.ts.map +1 -0
- package/dist/value/inference.js +17 -0
- package/dist/value/inference.js.map +1 -0
- package/dist/value/validator.d.ts +76 -0
- package/dist/value/validator.d.ts.map +1 -0
- package/dist/value/validator.js +111 -0
- package/dist/value/validator.js.map +1 -0
- package/package.json +95 -4
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* String shape types and operations.
|
|
18
|
+
*
|
|
19
|
+
* Defines the shape describing the textual values a resource may carry and its constraint types, and provides the
|
|
20
|
+
* factories stating them, mapping the [JSON string](https://datatracker.ietf.org/doc/html/rfc8259#section-7) type to
|
|
21
|
+
* [XSD 1.0](https://www.w3.org/TR/xmlschema-2/#built-in-datatypes) string and temporal datatypes. The
|
|
22
|
+
* {@link string} factory bounds a shape by length, lexical and enumeration constraints; the format factories fix the
|
|
23
|
+
* datatype and lexical pattern of a particular format.
|
|
24
|
+
*
|
|
25
|
+
* > [!IMPORTANT]
|
|
26
|
+
* > Contradictory constraints are rejected as the shape is built, so a shape that exists admits at least one value:
|
|
27
|
+
* > stating `minLength` above `maxLength` throws a {@link @grafctory/core!TraceError | TraceError}.
|
|
28
|
+
*
|
|
29
|
+
* | XSD Datatype ¹ | Factory | Description | Format |
|
|
30
|
+
* | ----------------- | ------------------- | ---------------------------------- | ----------------------------- |
|
|
31
|
+
* | [string][] | {@link string} | Unicode character sequence | |
|
|
32
|
+
* | string | {@link text} | Single-line plain text | |
|
|
33
|
+
* | string | {@link markdown} | [Markdown][] formatted text | |
|
|
34
|
+
* | string | {@link email} | [RFC 5321][] email address | |
|
|
35
|
+
* | string | {@link phone} | [ITU-T E.164][] telephone number | |
|
|
36
|
+
* | string | {@link iri} | [RFC 3987][] IRI reference | |
|
|
37
|
+
* | string | {@link url} | [RFC 3986][] hierarchical URL | |
|
|
38
|
+
* | string | {@link tag} | [BCP 47][] language tag | |
|
|
39
|
+
* | [gYear][] | {@link year} ² | [ISO 8601][iso-year] year | YYYY[Z/±hh:mm] |
|
|
40
|
+
* | [date][] | {@link date} | [ISO 8601][iso-date] date | YYYY-MM-DD[Z/±hh:mm] |
|
|
41
|
+
* | [time][] | {@link time} | [ISO 8601][iso-time] time | hh:mm:ss[.sss][Z/±hh:mm] |
|
|
42
|
+
* | [dateTime][] | {@link instant} | [ISO 8601][iso-datetime] date+time | YYYY-MM-DDThh:mm:ss[.sss][TZ] |
|
|
43
|
+
* | [dateTime][] | {@link timestamp} ³ | [ISO 8601][iso-datetime] UTC timestamp | YYYY-MM-DDThh:mm:ss.sssZ |
|
|
44
|
+
* | [duration][] | {@link duration} | [ISO 8601][iso-duration] duration | [-]PnYnMnDTnHnMnS |
|
|
45
|
+
*
|
|
46
|
+
* [string]: https://www.w3.org/TR/xmlschema-2/#string
|
|
47
|
+
* [anyURI]: https://www.w3.org/TR/xmlschema-2/#anyURI
|
|
48
|
+
* [gYear]: https://www.w3.org/TR/xmlschema-2/#gYear
|
|
49
|
+
* [date]: https://www.w3.org/TR/xmlschema-2/#date
|
|
50
|
+
* [time]: https://www.w3.org/TR/xmlschema-2/#time
|
|
51
|
+
* [dateTime]: https://www.w3.org/TR/xmlschema-2/#dateTime
|
|
52
|
+
* [duration]: https://www.w3.org/TR/xmlschema-2/#duration
|
|
53
|
+
*
|
|
54
|
+
* [Markdown]: https://commonmark.org/
|
|
55
|
+
* [RFC 5321]: https://datatracker.ietf.org/doc/html/rfc5321
|
|
56
|
+
* [RFC 3986]: https://datatracker.ietf.org/doc/html/rfc3986
|
|
57
|
+
* [RFC 3987]: https://datatracker.ietf.org/doc/html/rfc3987
|
|
58
|
+
* [ITU-T E.164]: https://www.itu.int/rec/T-REC-E.164
|
|
59
|
+
* [BCP 47]: https://www.rfc-editor.org/info/bcp47
|
|
60
|
+
* [iso-year]: https://en.wikipedia.org/wiki/ISO_8601#Years
|
|
61
|
+
* [iso-date]: https://en.wikipedia.org/wiki/ISO_8601#Dates
|
|
62
|
+
* [iso-time]: https://en.wikipedia.org/wiki/ISO_8601#Times
|
|
63
|
+
* [iso-datetime]: https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations
|
|
64
|
+
* [iso-duration]: https://en.wikipedia.org/wiki/ISO_8601#Durations
|
|
65
|
+
*
|
|
66
|
+
* ¹ XSD 1.0 datatypes are referenced by [RDF 1.1](https://www.w3.org/TR/rdf11-concepts/) and
|
|
67
|
+
* [JSON-LD 1.1](https://www.w3.org/TR/json-ld11/) as normative
|
|
68
|
+
*
|
|
69
|
+
* ² [XSD 1.1 Part 2 § D.3.4](https://www.w3.org/TR/xmlschema11-2/#deviantformats) permits optional timezone indicators
|
|
70
|
+
* for `gYear` as a deviation from ISO 8601
|
|
71
|
+
*
|
|
72
|
+
* ³ Requires exactly 3 fractional second digits (millisecond precision) and UTC timezone (`Z` only); typed as
|
|
73
|
+
* `xsd:dateTime` rather than the more specific `xsd:dateTimeStamp` for compatibility with SPARQL temporal functions,
|
|
74
|
+
* which are defined over `xsd:dateTime`
|
|
75
|
+
*
|
|
76
|
+
* > [!NOTE]
|
|
77
|
+
* > The temporal shorthands check the lexical form only: every field is bounded to its legal range and a timezone
|
|
78
|
+
* > offset to the ±14:00 range XSD admits, but fields are not cross-checked. As a limitation of that check, a day is
|
|
79
|
+
* > bounded to 31 whatever month it falls in, so a date the calendar does not carry, like `2026-02-31`, is admitted:
|
|
80
|
+
* > whoever needs a real date checks the day against the month.
|
|
81
|
+
*
|
|
82
|
+
* **Compatibility**
|
|
83
|
+
*
|
|
84
|
+
* | JSON | XSD | JavaScript |
|
|
85
|
+
* | ------------- | --------------------------- | ----------------------------- |
|
|
86
|
+
* | UTF-8 encoded | Unicode (XML 1.0 Char) | UTF-16 encoded (compatible) |
|
|
87
|
+
*
|
|
88
|
+
* **Defining string shapes**
|
|
89
|
+
*
|
|
90
|
+
* ```typescript
|
|
91
|
+
* import { string } from '@grafctory/blue/string';
|
|
92
|
+
*
|
|
93
|
+
* const value = string(); // unconstrained string
|
|
94
|
+
* const name = string({ minLength: 1, maxLength: 100 }); // length-constrained
|
|
95
|
+
* const code = string({ pattern: /^[A-Z]{3}-\d{4}$/ }); // pattern-constrained
|
|
96
|
+
* const status = string({ in: ["active", "inactive"] }); // enumeration-constrained
|
|
97
|
+
* const state = string("open", "closed"); // the same, stated as bare values
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* > [!NOTE]
|
|
101
|
+
* > An enumeration also narrows the value the shape describes: `status` admits `"active" | "inactive"`, not the whole
|
|
102
|
+
* > textual domain.
|
|
103
|
+
*
|
|
104
|
+
* **Specialised string factories**
|
|
105
|
+
*
|
|
106
|
+
* Predefined factories for common string formats:
|
|
107
|
+
*
|
|
108
|
+
* ```typescript
|
|
109
|
+
* import {
|
|
110
|
+
* text, markdown, email, phone, iri, url, tag, date, time, instant, timestamp, duration
|
|
111
|
+
* } from '@grafctory/blue/string';
|
|
112
|
+
*
|
|
113
|
+
* const label = text(); // single-line plain text
|
|
114
|
+
* const body = markdown(); // Markdown formatted text
|
|
115
|
+
* const contact = email(); // RFC 5321 email address
|
|
116
|
+
* const mobile = phone(); // ITU-T E.164 telephone number
|
|
117
|
+
* const identifier = iri(); // RFC 3987 IRI reference
|
|
118
|
+
* const link = url(); // RFC 3986 hierarchical URL
|
|
119
|
+
* const language = tag(); // BCP 47 language tag
|
|
120
|
+
* const birthday = date(); // ISO 8601 date (YYYY-MM-DD)
|
|
121
|
+
* const start = time(); // ISO 8601 time (hh:mm:ss)
|
|
122
|
+
* const created = instant(); // ISO 8601 datetime
|
|
123
|
+
* const modified = timestamp(); // ISO 8601 timestamp (millisecond precision, UTC)
|
|
124
|
+
* const validity = duration(); // ISO 8601 duration
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* **Using in resource shapes**
|
|
128
|
+
*
|
|
129
|
+
* ```typescript
|
|
130
|
+
* import { multiple, optional, required, resource } from '@grafctory/blue/resource';
|
|
131
|
+
* import { date, email, string } from '@grafctory/blue/string';
|
|
132
|
+
*
|
|
133
|
+
* const Person = resource({
|
|
134
|
+
* name: required(string({ minLength: 1 })),
|
|
135
|
+
* email: optional(email()),
|
|
136
|
+
* birthDate: optional(date())
|
|
137
|
+
* });
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @module
|
|
141
|
+
*
|
|
142
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc8259#section-7 RFC 8259 § 7 Strings}
|
|
143
|
+
* @see {@link https://www.w3.org/TR/xmlschema-2/#built-in-datatypes XSD 1.0 Part 2: Datatypes § 3 Built-in
|
|
144
|
+
* Datatypes}
|
|
145
|
+
*/
|
|
146
|
+
import { isString } from "@grafctory/core";
|
|
147
|
+
import { xsd } from "@grafctory/core/datatype";
|
|
148
|
+
import { TagPattern } from "@grafctory/core/language";
|
|
149
|
+
import { assemble } from "./assembler.js";
|
|
150
|
+
/**
|
|
151
|
+
* Lexical patterns admitting each subset of the IRI hierarchy.
|
|
152
|
+
*/
|
|
153
|
+
const IRIPatterns = {
|
|
154
|
+
browsable: /^[hH][tT][tT][pP][sS]?:\/\/[^\/?#\s]\S*$/,
|
|
155
|
+
hierarchical: /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\S*$/,
|
|
156
|
+
absolute: /^[a-zA-Z][a-zA-Z0-9+.-]*:\S+$/,
|
|
157
|
+
internal: /^(?:[a-zA-Z][a-zA-Z0-9+.-]*:\S+|\/\S*)$/,
|
|
158
|
+
relative: /^\S+$/
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Admits a four-digit calendar year, as the XSD `yearFrag` production does for the years the shorthands cover.
|
|
162
|
+
*/
|
|
163
|
+
const YearFrag = "\\d{4}";
|
|
164
|
+
/**
|
|
165
|
+
* Admits a calendar month, as the XSD `monthFrag` production does.
|
|
166
|
+
*/
|
|
167
|
+
const MonthFrag = "(?:0[1-9]|1[0-2])";
|
|
168
|
+
/**
|
|
169
|
+
* Admits a calendar day, as the XSD `dayFrag` production does, irrespective of the length of the month it falls in.
|
|
170
|
+
*/
|
|
171
|
+
const DayFrag = "(?:0[1-9]|[12]\\d|3[01])";
|
|
172
|
+
/**
|
|
173
|
+
* Admits an hour of the day, as the XSD `hourFrag` production does.
|
|
174
|
+
*/
|
|
175
|
+
const HourFrag = "(?:[01]\\d|2[0-3])";
|
|
176
|
+
/**
|
|
177
|
+
* Admits a minute of the hour, as the XSD `minuteFrag` production does.
|
|
178
|
+
*/
|
|
179
|
+
const MinuteFrag = "[0-5]\\d";
|
|
180
|
+
/**
|
|
181
|
+
* Admits a second of the minute, optionally fractional, as the XSD `secondFrag` production does.
|
|
182
|
+
*/
|
|
183
|
+
const SecondFrag = "[0-5]\\d(?:\\.\\d+)?";
|
|
184
|
+
/**
|
|
185
|
+
* Admits the `24:00:00` end-of-day form, as the XSD `endOfDayFrag` production does.
|
|
186
|
+
*/
|
|
187
|
+
const EndOfDayFrag = "24:00:00(?:\\.0+)?";
|
|
188
|
+
/**
|
|
189
|
+
* Admits a timezone offset bounded to the ±14:00 range, as the XSD `timezoneFrag` production does.
|
|
190
|
+
*/
|
|
191
|
+
const TimezoneFrag = `(?:Z|[+-](?:(?:0\\d|1[0-3]):${MinuteFrag}|14:00))`;
|
|
192
|
+
/**
|
|
193
|
+
* Admits a time of day, as the lexical space of `xsd:time` does before its optional timezone.
|
|
194
|
+
*/
|
|
195
|
+
const TimeFrag = `(?:${HourFrag}:${MinuteFrag}:${SecondFrag}|${EndOfDayFrag})`;
|
|
196
|
+
/**
|
|
197
|
+
* Creates a string shape.
|
|
198
|
+
*/
|
|
199
|
+
export function string(...args) {
|
|
200
|
+
const [head] = args;
|
|
201
|
+
return assemble(isString(head) ? { in: args.filter(isString) } : { ...head });
|
|
202
|
+
}
|
|
203
|
+
//// Textual Shorthands //////////////////////////////////////////////////////////////////////////////////////////////
|
|
204
|
+
/**
|
|
205
|
+
* Creates a shape for single-line plain text values.
|
|
206
|
+
*
|
|
207
|
+
* Fixes the datatype to `xsd:string` and admits space-normalised single-line content: at least one non-whitespace
|
|
208
|
+
* character, no line breaks or tabs, no leading or trailing whitespace, and single spaces between words. Reach for it
|
|
209
|
+
* for labels, names and other short unformatted values that must survive rendering in any layout; reach for
|
|
210
|
+
* {@link markdown} where the content carries formatting or spans multiple lines.
|
|
211
|
+
*
|
|
212
|
+
* @param constraints Optional {@link StringLengthConstraints length bounds}
|
|
213
|
+
*
|
|
214
|
+
* @returns An immutable shape admitting single-line plain text
|
|
215
|
+
*
|
|
216
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
217
|
+
*/
|
|
218
|
+
export function text(constraints) {
|
|
219
|
+
return assemble({
|
|
220
|
+
datatype: xsd.string,
|
|
221
|
+
pattern: /^\S+(?: \S+)*$/,
|
|
222
|
+
...constraints
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Creates a shape for Markdown text values.
|
|
227
|
+
*
|
|
228
|
+
* Fixes the datatype to `xsd:string` and admits any string within the stated length bounds. Whitespace carries meaning
|
|
229
|
+
* throughout Markdown, from indentation and blank lines to the trailing spaces that encode a hard break, so no lexical
|
|
230
|
+
* constraint is imposed and authored content survives ingestion verbatim. Reach for it for descriptions, abstracts and
|
|
231
|
+
* other long-form values whose formatting is meaningful; reach for {@link text} where the content must stay a single
|
|
232
|
+
* unformatted line.
|
|
233
|
+
*
|
|
234
|
+
* @param constraints Optional {@link StringLengthConstraints length bounds}
|
|
235
|
+
*
|
|
236
|
+
* @returns An immutable shape admitting Markdown text
|
|
237
|
+
*
|
|
238
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
239
|
+
*
|
|
240
|
+
* @see {@link https://commonmark.org/ CommonMark Spec}
|
|
241
|
+
*/
|
|
242
|
+
export function markdown(constraints) {
|
|
243
|
+
return assemble({
|
|
244
|
+
datatype: xsd.string,
|
|
245
|
+
...constraints
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Creates a shape for email address values.
|
|
250
|
+
*
|
|
251
|
+
* Fixes the datatype to `xsd:string`.
|
|
252
|
+
*
|
|
253
|
+
* @typeParam C The stated constraints
|
|
254
|
+
*
|
|
255
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
256
|
+
*
|
|
257
|
+
* @returns An immutable shape admitting email addresses, narrowed to the values the constraints enumerate
|
|
258
|
+
*
|
|
259
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
260
|
+
*
|
|
261
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc5321 RFC 5321 - Simple Mail Transfer Protocol}
|
|
262
|
+
*/
|
|
263
|
+
export function email(constraints) {
|
|
264
|
+
return assemble({
|
|
265
|
+
datatype: xsd.string,
|
|
266
|
+
pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
267
|
+
...constraints
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Creates a shape for telephone number values.
|
|
272
|
+
*
|
|
273
|
+
* Fixes the datatype to `xsd:string` and admits numbers in ITU-T E.164 notation: a leading `+`, a non-zero country code
|
|
274
|
+
* digit, and up to 14 further digits, with no spaces or separators (for example, `+15555550123`).
|
|
275
|
+
*
|
|
276
|
+
* @typeParam C The stated constraints
|
|
277
|
+
*
|
|
278
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
279
|
+
*
|
|
280
|
+
* @returns An immutable shape admitting E.164 telephone numbers, narrowed to the values the constraints enumerate
|
|
281
|
+
*
|
|
282
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
283
|
+
*
|
|
284
|
+
* @see {@link https://www.itu.int/rec/T-REC-E.164 ITU-T E.164 - International public telecommunication numbering plan}
|
|
285
|
+
*/
|
|
286
|
+
export function phone(constraints) {
|
|
287
|
+
return assemble({
|
|
288
|
+
datatype: xsd.string,
|
|
289
|
+
pattern: /^\+[1-9]\d{1,14}$/,
|
|
290
|
+
...constraints
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Creates a shape for Internationalized Resource Identifier values.
|
|
295
|
+
*
|
|
296
|
+
* Fixes the datatype to `xsd:string`. IRIs generalise URIs (RFC 3986) and URLs by admitting the full Unicode character
|
|
297
|
+
* set beyond ASCII; the {@link Variant | variant} states which subset of the IRI hierarchy is accepted:
|
|
298
|
+
* `browsable` (HTTP and HTTPS links a browser can open), `hierarchical` (a scheme followed by an absolute path, with
|
|
299
|
+
* or without authority), `absolute` (scheme-based URIs/IRIs), `internal` (absolute or root-relative), or `relative`
|
|
300
|
+
* (any valid reference, including relative paths such as `../path` or `path`; the default).
|
|
301
|
+
*
|
|
302
|
+
* As a limitation, variants are told apart by their prefix rather than parsed against the full RFC 3987 grammar: the
|
|
303
|
+
* scheme, the leading slashes and, for `browsable`, the presence of a host. `relative` admits any whitespace-free
|
|
304
|
+
* sequence, and the stricter variants leave the rest of the reference unvalidated.
|
|
305
|
+
*
|
|
306
|
+
* @typeParam C The stated constraints
|
|
307
|
+
*
|
|
308
|
+
* @param constraints Optional {@link StringValueConstraints value constraints} and IRI {@link Variant | variant}
|
|
309
|
+
*
|
|
310
|
+
* @returns An immutable shape admitting IRIs of the stated variant, narrowed to the values the constraints enumerate
|
|
311
|
+
*
|
|
312
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
313
|
+
*
|
|
314
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc3987 RFC 3987 - Internationalized Resource Identifiers}
|
|
315
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc3986 RFC 3986 - URI Generic Syntax}
|
|
316
|
+
*/
|
|
317
|
+
export function iri(constraints) {
|
|
318
|
+
const { variant = "relative", ...values } = constraints ?? {};
|
|
319
|
+
return assemble({
|
|
320
|
+
datatype: xsd.string,
|
|
321
|
+
pattern: IRIPatterns[variant],
|
|
322
|
+
...values
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Creates a shape for hierarchical URL values.
|
|
327
|
+
*
|
|
328
|
+
* Fixes the datatype to `xsd:string` and admits only URLs with a scheme and an absolute path, authority optional (for
|
|
329
|
+
* example, `https://example.net/path` or `base:/path`), as {@link iri} does with `variant: "hierarchical"` and under
|
|
330
|
+
* the same limitation: only the scheme and the leading slash are checked. Page addresses a browser can open take
|
|
331
|
+
* {@link iri} with `variant: "browsable"` instead, which admits only HTTP and HTTPS links.
|
|
332
|
+
*
|
|
333
|
+
* @typeParam C The stated constraints
|
|
334
|
+
*
|
|
335
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
336
|
+
*
|
|
337
|
+
* @returns An immutable shape admitting hierarchical URLs, narrowed to the values the constraints enumerate
|
|
338
|
+
*
|
|
339
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
340
|
+
*
|
|
341
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc3986 RFC 3986 - URI Generic Syntax}
|
|
342
|
+
*/
|
|
343
|
+
export function url(constraints) {
|
|
344
|
+
return assemble({
|
|
345
|
+
datatype: xsd.string,
|
|
346
|
+
pattern: IRIPatterns.hierarchical,
|
|
347
|
+
...constraints
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Creates a shape for language tag values.
|
|
352
|
+
*
|
|
353
|
+
* Fixes the datatype to `xsd:string` and admits BCP 47 language tags identifying a natural language, from a bare
|
|
354
|
+
* language subtag to a fully qualified tag carrying script, region, variant, extension and private-use subtags (for
|
|
355
|
+
* example, `en`, `fr-CA`, `zh-Hans-CN`); subtags are matched case-insensitively, as the standard prescribes, and
|
|
356
|
+
* grandfathered tags are not accepted.
|
|
357
|
+
*
|
|
358
|
+
* Reach for it wherever a member records the language of a value; language *ranges*, which select values rather than
|
|
359
|
+
* identify a language, are constrained by {@link dictionary!DictionaryConstraints.languageIn | languageIn} instead.
|
|
360
|
+
*
|
|
361
|
+
* @typeParam C The stated constraints
|
|
362
|
+
*
|
|
363
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
364
|
+
*
|
|
365
|
+
* @returns An immutable shape admitting language tags, narrowed to the values the constraints enumerate
|
|
366
|
+
*
|
|
367
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
368
|
+
*
|
|
369
|
+
* @see {@link https://www.rfc-editor.org/info/bcp47 BCP 47 - Tags for Identifying Languages}
|
|
370
|
+
*/
|
|
371
|
+
export function tag(constraints) {
|
|
372
|
+
return assemble({
|
|
373
|
+
datatype: xsd.string,
|
|
374
|
+
pattern: TagPattern,
|
|
375
|
+
...constraints
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
//// Temporal Shorthands /////////////////////////////////////////////////////////////////////////////////////////////
|
|
379
|
+
/**
|
|
380
|
+
* Creates a shape for ISO 8601 year values (YYYY).
|
|
381
|
+
*
|
|
382
|
+
* Fixes the datatype to `xsd:gYear` and admits an optional timezone indicator (`Z` for UTC or a ±hh:mm offset bounded
|
|
383
|
+
* to the ±14:00 range).
|
|
384
|
+
*
|
|
385
|
+
* @typeParam C The stated constraints
|
|
386
|
+
*
|
|
387
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
388
|
+
*
|
|
389
|
+
* @returns An immutable shape admitting ISO 8601 years, narrowed to the values the constraints enumerate
|
|
390
|
+
*
|
|
391
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
392
|
+
*
|
|
393
|
+
* @remarks
|
|
394
|
+
*
|
|
395
|
+
* XSD permits timezone indicators for gYear as a deviation from ISO 8601.
|
|
396
|
+
*
|
|
397
|
+
* @see {@link https://www.w3.org/TR/xmlschema-2/#gYear XSD 1.0 Part 2: Datatypes § 3.2.11 gYear}
|
|
398
|
+
*/
|
|
399
|
+
export function year(constraints) {
|
|
400
|
+
return assemble({
|
|
401
|
+
datatype: xsd.gYear,
|
|
402
|
+
pattern: `^${YearFrag}${TimezoneFrag}?$`,
|
|
403
|
+
...constraints
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Creates a shape for ISO 8601 calendar date values (YYYY-MM-DD).
|
|
408
|
+
*
|
|
409
|
+
* Fixes the datatype to `xsd:date` and bounds the month, the day and the optional timezone offset to their legal
|
|
410
|
+
* ranges. As a limitation of the lexical check, a day is bounded to 31 whatever month it falls in, so `2026-02-31` is
|
|
411
|
+
* admitted: callers needing a date the calendar carries check the day against the month themselves.
|
|
412
|
+
*
|
|
413
|
+
* @typeParam C The stated constraints
|
|
414
|
+
*
|
|
415
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
416
|
+
*
|
|
417
|
+
* @returns An immutable shape admitting ISO 8601 dates, narrowed to the values the constraints enumerate
|
|
418
|
+
*
|
|
419
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
420
|
+
*
|
|
421
|
+
* @see {@link https://www.w3.org/TR/xmlschema-2/#date XSD 1.0 Part 2: Datatypes § 3.2.9 date}
|
|
422
|
+
*/
|
|
423
|
+
export function date(constraints) {
|
|
424
|
+
return assemble({
|
|
425
|
+
datatype: xsd.date,
|
|
426
|
+
pattern: `^${YearFrag}-${MonthFrag}-${DayFrag}${TimezoneFrag}?$`,
|
|
427
|
+
...constraints
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Creates a shape for ISO 8601 time of day values (hh:mm:ss).
|
|
432
|
+
*
|
|
433
|
+
* Fixes the datatype to `xsd:time` and bounds each field and the optional timezone offset to its legal range, with
|
|
434
|
+
* seconds optionally fractional. The `24:00:00` end-of-day form XSD defines is admitted, carrying no fraction beyond
|
|
435
|
+
* zeroes.
|
|
436
|
+
*
|
|
437
|
+
* @typeParam C The stated constraints
|
|
438
|
+
*
|
|
439
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
440
|
+
*
|
|
441
|
+
* @returns An immutable shape admitting ISO 8601 times, narrowed to the values the constraints enumerate
|
|
442
|
+
*
|
|
443
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
444
|
+
*
|
|
445
|
+
* @see {@link https://www.w3.org/TR/xmlschema-2/#time XSD 1.0 Part 2: Datatypes § 3.2.8 time}
|
|
446
|
+
*/
|
|
447
|
+
export function time(constraints) {
|
|
448
|
+
return assemble({
|
|
449
|
+
datatype: xsd.time,
|
|
450
|
+
pattern: `^${TimeFrag}${TimezoneFrag}?$`,
|
|
451
|
+
...constraints
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Creates a shape for ISO 8601 date and time values (YYYY-MM-DDThh:mm:ss).
|
|
456
|
+
*
|
|
457
|
+
* Fixes the datatype to `xsd:dateTime` and bounds each field and the optional timezone offset to its legal range,
|
|
458
|
+
* under the same end-of-day rule as {@link time} and the same day limitation as {@link date}.
|
|
459
|
+
*
|
|
460
|
+
* @typeParam C The stated constraints
|
|
461
|
+
*
|
|
462
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
463
|
+
*
|
|
464
|
+
* @returns An immutable shape admitting ISO 8601 date and time values, narrowed to the values the constraints
|
|
465
|
+
* enumerate
|
|
466
|
+
*
|
|
467
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
468
|
+
*
|
|
469
|
+
* @see {@link https://www.w3.org/TR/xmlschema-2/#dateTime XSD 1.0 Part 2: Datatypes § 3.2.7 dateTime}
|
|
470
|
+
*/
|
|
471
|
+
export function instant(constraints) {
|
|
472
|
+
return assemble({
|
|
473
|
+
datatype: xsd.dateTime,
|
|
474
|
+
pattern: `^${YearFrag}-${MonthFrag}-${DayFrag}T${TimeFrag}${TimezoneFrag}?$`,
|
|
475
|
+
...constraints
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Creates a shape for UTC timestamp values with millisecond precision (YYYY-MM-DDThh:mm:ss.sssZ).
|
|
480
|
+
*
|
|
481
|
+
* Requires exactly 3 fractional second digits and the UTC timezone (`Z` only), bounding each field to its legal range
|
|
482
|
+
* under the same day limitation as {@link date}; unlike {@link instant}, it admits no `24:00:00` end-of-day form, so a
|
|
483
|
+
* timestamp always states a time of day. Fixes the datatype to `xsd:dateTime` rather than the more specific
|
|
484
|
+
* `xsd:dateTimeStamp`, so values stay compatible with SPARQL temporal functions, which are defined over
|
|
485
|
+
* `xsd:dateTime`.
|
|
486
|
+
*
|
|
487
|
+
* @typeParam C The stated constraints
|
|
488
|
+
*
|
|
489
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
490
|
+
*
|
|
491
|
+
* @returns An immutable shape admitting UTC timestamps, narrowed to the values the constraints enumerate
|
|
492
|
+
*
|
|
493
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
494
|
+
*
|
|
495
|
+
* @see {@link https://www.w3.org/TR/xmlschema-2/#dateTime XSD 1.0 Part 2: Datatypes § 3.2.7 dateTime}
|
|
496
|
+
*/
|
|
497
|
+
export function timestamp(constraints) {
|
|
498
|
+
return assemble({
|
|
499
|
+
datatype: xsd.dateTime,
|
|
500
|
+
pattern: `^${YearFrag}-${MonthFrag}-${DayFrag}T${HourFrag}:${MinuteFrag}:[0-5]\\d\\.\\d{3}Z$`,
|
|
501
|
+
...constraints
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Creates a shape for ISO 8601 duration values ([-]PnYnMnDTnHnMnS).
|
|
506
|
+
*
|
|
507
|
+
* Fixes the datatype to `xsd:duration` and requires at least one component, so that neither `P` nor a `T` designator
|
|
508
|
+
* carrying no time component is admitted. Components are stated in descending order of magnitude, and only seconds
|
|
509
|
+
* take a fraction.
|
|
510
|
+
*
|
|
511
|
+
* @typeParam C The stated constraints
|
|
512
|
+
*
|
|
513
|
+
* @param constraints Optional {@link StringValueConstraints value constraints}
|
|
514
|
+
*
|
|
515
|
+
* @returns An immutable shape admitting ISO 8601 durations, narrowed to the values the constraints enumerate
|
|
516
|
+
*
|
|
517
|
+
* @throws {@link @grafctory/core!TraceError | TraceError} Where the stated constraints contradict one another
|
|
518
|
+
*
|
|
519
|
+
* @see {@link https://www.w3.org/TR/xmlschema-2/#duration XSD 1.0 Part 2: Datatypes § 3.2.6 duration}
|
|
520
|
+
*/
|
|
521
|
+
export function duration(constraints) {
|
|
522
|
+
return assemble({
|
|
523
|
+
datatype: xsd.duration,
|
|
524
|
+
pattern: /^-?P(?!$)(?:\d+Y)?(?:\d+M)?(?:\d+D)?(?:T(?!$)(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?$/,
|
|
525
|
+
...constraints
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/string/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiIG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAItD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG1C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AAE,GAAG,CAAC,QAAQ;QACtB,OAAO,EAAE,IAAI,QAAQ,IAAI,SAAS,IAAI,OAAO,IAAI,QAAQ,IAAI,UAAU,sBAAsB;QAE7F,GAAG,WAAW;KAEd,CAAC,CAAC;AAEJ,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,QAAQ,CAA8C,WAAe;IAEpF,OAAO,QAAQ,CAAmB;QAEjC,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,OAAO,EAAE,uFAAuF;QAEhG,GAAG,WAAW;KAEd,CAAC,CAAC;AAEJ,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* String value validation.
|
|
3
|
+
*
|
|
4
|
+
* Holds a value to what a string shape admits and reports everything wrong with it at once, keyed by the facet at
|
|
5
|
+
* fault, at the strictness the caller asks for: a stored value or a retrieval placeholder.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { type Optional } from "@grafctory/core";
|
|
10
|
+
import { type Trace } from "@grafctory/core/trace";
|
|
11
|
+
import { type Scope } from "../value/validator.js";
|
|
12
|
+
import { type StringShape } from "./index.js";
|
|
13
|
+
/**
|
|
14
|
+
* Validates values against a string shape.
|
|
15
|
+
*
|
|
16
|
+
* Reports each value that is not a string as a `{type}` violation and each string that breaks a constraint under its
|
|
17
|
+
* own facet, keying every element violation by its index, so that a caller may tell which value failed and why;
|
|
18
|
+
* membership over the whole set (`hasValue`) is reported as a leading bare message.
|
|
19
|
+
*
|
|
20
|
+
* @param values The values to validate
|
|
21
|
+
* @param shape The shape the values are matched against
|
|
22
|
+
* @param opts Validation options
|
|
23
|
+
* @param opts.scope The {@link Scope | strictness} the shape is enforced at, defaulting to `"state"`
|
|
24
|
+
*
|
|
25
|
+
* @returns A trace of the violations found, or `undefined` where every value matches `shape`
|
|
26
|
+
*/
|
|
27
|
+
export declare const validateString: (values: readonly unknown[], shape: StringShape, opts?: {
|
|
28
|
+
scope?: Scope;
|
|
29
|
+
}) => Optional<Trace>;
|
|
30
|
+
//# sourceMappingURL=validator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/string/validator.ts"],"names":[],"mappings":"AAgBA;;;;;;;GAOG;AAEH,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE1D,OAAO,EAA0C,KAAK,KAAK,EAA4C,MACjG,uBAAuB,CAAC;AAC9B,OAAO,EAAE,KAAK,KAAK,EAAU,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAG9C;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,cAAc,EAAE,CAAC,MAAM,EAAE,SAAS,OAAO,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE;IAEpF,KAAK,CAAC,EAAE,KAAK,CAAA;CAEb,KAAK,QAAQ,CAAC,KAAK,CAAwB,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2025-2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* String value validation.
|
|
18
|
+
*
|
|
19
|
+
* Holds a value to what a string shape admits and reports everything wrong with it at once, keyed by the facet at
|
|
20
|
+
* fault, at the strictness the caller asks for: a stored value or a retrieval placeholder.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
import { isString } from "@grafctory/core";
|
|
25
|
+
import { isAtomic } from "@grafctory/qest/model";
|
|
26
|
+
import { all, array, domain, length, pass, test, type, values as contains } from "@grafctory/core/trace";
|
|
27
|
+
import { scoped } from "../value/validator.js";
|
|
28
|
+
/**
|
|
29
|
+
* Validates values against a string shape.
|
|
30
|
+
*
|
|
31
|
+
* Reports each value that is not a string as a `{type}` violation and each string that breaks a constraint under its
|
|
32
|
+
* own facet, keying every element violation by its index, so that a caller may tell which value failed and why;
|
|
33
|
+
* membership over the whole set (`hasValue`) is reported as a leading bare message.
|
|
34
|
+
*
|
|
35
|
+
* @param values The values to validate
|
|
36
|
+
* @param shape The shape the values are matched against
|
|
37
|
+
* @param opts Validation options
|
|
38
|
+
* @param opts.scope The {@link Scope | strictness} the shape is enforced at, defaulting to `"state"`
|
|
39
|
+
*
|
|
40
|
+
* @returns A trace of the violations found, or `undefined` where every value matches `shape`
|
|
41
|
+
*/
|
|
42
|
+
export const validateString = scoped(state, model);
|
|
43
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
44
|
+
/**
|
|
45
|
+
* Every constraint: a legal element of the shape's domain.
|
|
46
|
+
*/
|
|
47
|
+
function state({ minLength, maxLength, pattern, in: allowed, hasValue: required }) {
|
|
48
|
+
return array(type(isString, all(length(minLength, maxLength), domain(allowed), format(pattern))), contains(required));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The form alone: a retrieval placeholder asks for the value as it stands and carries none of its own.
|
|
52
|
+
*/
|
|
53
|
+
function model({}) {
|
|
54
|
+
return array(type(isAtomic));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Matches a string against the lexical pattern a shape states, admitting every string where it states none.
|
|
58
|
+
*/
|
|
59
|
+
function format(pattern) {
|
|
60
|
+
if (pattern === undefined) {
|
|
61
|
+
return pass;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const regex = new RegExp(pattern);
|
|
65
|
+
const mismatched = [`{pattern} expected string matching </${pattern}/>`];
|
|
66
|
+
return test(value => regex.test(value) || mismatched);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=validator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/string/validator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAiB,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAc,IAAI,EAAkB,MAAM,IAAI,QAAQ,EAAE,MACjG,uBAAuB,CAAC;AAC9B,OAAO,EAAc,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAI3D;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,cAAc,GAIH,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAG7C,wHAAwH;AAExH;;GAEG;AACH,SAAS,KAAK,CAAC,EAEd,SAAS,EACT,SAAS,EAET,OAAO,EAEP,EAAE,EAAE,OAAO,EACX,QAAQ,EAAE,QAAQ,EAEL;IAEb,OAAO,KAAK,CACX,IAAI,CAAC,QAAQ,EACZ,GAAG,CACF,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,EAC5B,MAAM,CAAC,OAAO,CAAC,EACf,MAAM,CAAC,OAAO,CAAC,CACf,CACD,EACD,QAAQ,CAAC,QAAQ,CAAC,CAClB,CAAC;AAEH,CAAC;AAED;;GAEG;AACH,SAAS,KAAK,CAAC,EAAe;IAE7B,OAAO,KAAK,CACX,IAAI,C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