@based/schema 5.0.0-alpha.23 → 5.0.0-alpha.24

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.
@@ -115,6 +115,9 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
115
115
  default: schemaProp.default ?? DEFAULT_MAP[typeIndex],
116
116
  prop: isseparate ? ++result.cnt : 0,
117
117
  };
118
+ if (schemaProp.transform) {
119
+ prop.transform = schemaProp.transform;
120
+ }
118
121
  if (schemaProp.max !== undefined) {
119
122
  prop.max = parseMinMaxStep(schemaProp.max);
120
123
  }
@@ -196,6 +199,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
196
199
  }
197
200
  }
198
201
  let len = 2;
202
+ let biggestSeperatePropDefault = 0;
199
203
  for (const f of vals) {
200
204
  if (f.separate) {
201
205
  len += 2;
@@ -203,9 +207,15 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
203
207
  if (f.default !== undefined) {
204
208
  result.hasSeperateDefaults = true;
205
209
  if (!result.seperateDefaults) {
206
- // result.seperateDefaults = []
210
+ result.seperateDefaults = {
211
+ props: new Map(),
212
+ bufferTmp: new Uint8Array(),
213
+ };
214
+ }
215
+ result.seperateDefaults.props.set(f.prop, f);
216
+ if (f.prop > biggestSeperatePropDefault) {
217
+ biggestSeperatePropDefault = f.prop;
207
218
  }
208
- // result.seperateDefaults.push(f)
209
219
  }
210
220
  }
211
221
  else {
@@ -218,6 +228,9 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = cr
218
228
  setByPath(result.tree, f.path, f);
219
229
  }
220
230
  }
231
+ if (result.hasSeperateDefaults) {
232
+ result.seperateDefaults.bufferTmp = new Uint8Array(biggestSeperatePropDefault + 1);
233
+ }
221
234
  result.mainEmpty = fillEmptyMain(vals, result.mainLen);
222
235
  result.mainEmptyAllZeroes = isZeroes(result.mainEmpty);
223
236
  if (result.separateSortText > 0) {
@@ -1,5 +1,6 @@
1
1
  import type { LangCode, SchemaLocales } from '../index.js';
2
2
  import { Validation } from './validation.js';
3
+ export type Transform = (type: 'create' | 'update' | 'read' | 'filter' | 'search', value: any) => any;
3
4
  export declare const NULL = 0;
4
5
  export declare const TIMESTAMP = 1;
5
6
  export declare const NUMBER = 4;
@@ -82,6 +83,7 @@ export type PropDef = {
82
83
  enum?: any[];
83
84
  dependent?: boolean;
84
85
  validation: Validation;
86
+ transform?: Transform;
85
87
  default: any;
86
88
  edgeMainLen?: 0;
87
89
  reverseEnum?: {
@@ -182,6 +182,7 @@ export const VALIDATION_MAP = {
182
182
  return true;
183
183
  },
184
184
  [TYPE_INDEX_MAP.json]: (value) => {
185
+ // mep
185
186
  return true;
186
187
  },
187
188
  [TYPE_INDEX_MAP.microbuffer]: (value) => {
@@ -58,6 +58,9 @@ const shared = {
58
58
  validation(val) {
59
59
  expectFunction(val);
60
60
  },
61
+ transform(val) {
62
+ expectFunction(val);
63
+ },
61
64
  };
62
65
  function propParser(required, optional, allowShorthand) {
63
66
  return (prop, ctx) => {
@@ -132,6 +135,28 @@ export const isDefault = (val, prop, ctx) => {
132
135
  max: parseMinMaxStep(prop.max),
133
136
  min: parseMinMaxStep(prop.min),
134
137
  };
138
+ if (prop.type === 'text') {
139
+ if (typeof val === 'object') {
140
+ for (const key in val) {
141
+ if (!ctx.schema.locales || !(key in ctx.schema.locales)) {
142
+ throw new Error(`Incorrect default for type "text" lang "${key}"`);
143
+ }
144
+ if (!validation(val[key], propDef)) {
145
+ throw new Error(`Incorrect default for type "text" lang "${key}"`);
146
+ }
147
+ }
148
+ }
149
+ else {
150
+ if (!validation(val, propDef)) {
151
+ throw new Error(`Incorrect default for type "text"`);
152
+ }
153
+ val = {};
154
+ for (const key in ctx.schema.locales) {
155
+ val[key] = ctx.schema.locales[key];
156
+ }
157
+ }
158
+ return val;
159
+ }
135
160
  if (!validation(val, propDef)) {
136
161
  throw new Error(`Incorrect default for type "${prop.type ?? 'enum'}"`);
137
162
  }
@@ -313,13 +338,11 @@ p.text = propParser({
313
338
  },
314
339
  }, {
315
340
  compression(val) {
316
- // return the actualy string!
317
341
  return val;
318
342
  },
319
343
  format: binaryOpts.format,
320
- default(_val, _prop) {
321
- // console.warn('MAKE DEFAULT VALUE FOR TEXT')
322
- return true;
344
+ default(val, prop, ctx) {
345
+ return isDefault(val, prop, ctx);
323
346
  },
324
347
  }, 0);
325
348
  p.timestamp = propParser(STUB, {
package/dist/serialize.js CHANGED
@@ -120,36 +120,50 @@ const handleSingleValue = (ops, val, obj, prev, fromObject, key) => {
120
120
  }
121
121
  };
122
122
  const encodeKey = (key, schemaBuffer) => {
123
- let address = schemaBuffer.dictMap[key];
123
+ let dictKey = schemaBuffer.dictMap[key];
124
124
  // if len == 1 never from address
125
- if (!address) {
125
+ if (!dictKey) {
126
+ dictKey = {
127
+ changed: 0,
128
+ address: 0,
129
+ // used: [],
130
+ };
126
131
  // pessimistically assume 4 bytes per char for UTF-8 to be safe.
127
132
  ensureCapacity(1 + key.length * 4);
128
- address = schemaBuffer.len;
133
+ dictKey.address = schemaBuffer.len;
129
134
  schemaBuffer.len += 1;
130
135
  const r = ENCODER.encodeInto(key, schemaBuffer.buf.subarray(schemaBuffer.len));
131
- schemaBuffer.buf[address] = r.written + KEY_OPTS;
136
+ schemaBuffer.buf[dictKey.address] = r.written + KEY_OPTS;
132
137
  schemaBuffer.len += r.written;
133
- schemaBuffer.dictMap[key] = address;
138
+ // USED is the problem now
139
+ schemaBuffer.dictMapArr.push(dictKey);
140
+ schemaBuffer.dictMap[key] = dictKey;
134
141
  }
135
142
  else {
136
143
  ensureCapacity(4);
137
- if (address > 65025) {
144
+ // updated address? maybe
145
+ const dictMapUsed = { address: schemaBuffer.len, key: dictKey };
146
+ schemaBuffer.dictMapUsed.push(dictMapUsed);
147
+ // used can be handled differently - also pass to
148
+ // dictKey.used.push(dictMapUsed)
149
+ // console.log('USE KEY!', key)
150
+ // have to check this to correct - correctly
151
+ if (dictKey.address > 65025) {
138
152
  schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_3_BYTES;
139
153
  schemaBuffer.len += 1;
140
- writeUint24(schemaBuffer.buf, address, schemaBuffer.len);
154
+ writeUint24(schemaBuffer.buf, dictKey.address, schemaBuffer.len);
141
155
  schemaBuffer.len += 3;
142
156
  }
143
- else if (address > 255) {
157
+ else if (dictKey.address > 255) {
144
158
  schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_2_BYTES;
145
159
  schemaBuffer.len += 1;
146
- writeUint16(schemaBuffer.buf, address, schemaBuffer.len);
160
+ writeUint16(schemaBuffer.buf, dictKey.address, schemaBuffer.len);
147
161
  schemaBuffer.len += 2;
148
162
  }
149
163
  else {
150
164
  schemaBuffer.buf[schemaBuffer.len] = KEY_ADDRESS_1_BYTE;
151
165
  schemaBuffer.len += 1;
152
- schemaBuffer.buf[schemaBuffer.len] = address;
166
+ schemaBuffer.buf[schemaBuffer.len] = dictKey.address;
153
167
  schemaBuffer.len += 1;
154
168
  }
155
169
  }
@@ -173,7 +187,7 @@ const walk = (opts, obj, prev, prev2, fromObject, schemaBuffer) => {
173
187
  schemaBuffer.buf[schemaBuffer.len++] = isArray ? ARRAY : OBJECT;
174
188
  }
175
189
  let sizeIndex = schemaBuffer.len;
176
- schemaBuffer.len += 5;
190
+ schemaBuffer.len += 4;
177
191
  if (isArray) {
178
192
  const len = obj.length;
179
193
  ensureCapacity(2 * len + 2);
@@ -270,30 +284,67 @@ const walk = (opts, obj, prev, prev2, fromObject, schemaBuffer) => {
270
284
  }
271
285
  }
272
286
  let size = schemaBuffer.len - start;
273
- // 3
274
- // if (size < 252) {
275
- // console.log('FLAP>', size)
276
- // schemaBuffer.buf[sizeIndex] = size + 3
277
- // schemaBuffer.buf.set(
278
- // schemaBuffer.buf.subarray(sizeIndex + 4, sizeIndex + size),
279
- // sizeIndex + 1,
280
- // )
281
- // schemaBuffer.len -= 4
282
- // } else {
283
- schemaBuffer.buf[sizeIndex] = 0; // means 4
284
- writeUint32(schemaBuffer.buf, size, sizeIndex + 1);
285
- // }
287
+ // 3 different sizes? 3, 2, 1 ?
288
+ if (size < 252) {
289
+ schemaBuffer.keyChangeIndex++;
290
+ schemaBuffer.buf[sizeIndex] = size; // + 3 - 3
291
+ for (let i = schemaBuffer.dictMapArr.length - 1; i > -1; i--) {
292
+ const keyDict = schemaBuffer.dictMapArr[i];
293
+ if (keyDict.address < start) {
294
+ break;
295
+ }
296
+ else {
297
+ keyDict.changed = schemaBuffer.keyChangeIndex;
298
+ keyDict.address -= 3;
299
+ }
300
+ }
301
+ for (let i = schemaBuffer.dictMapUsed.length - 1; i > -1; i--) {
302
+ const keyDictUsed = schemaBuffer.dictMapUsed[i];
303
+ if (keyDictUsed.address < start) {
304
+ break;
305
+ }
306
+ else {
307
+ const keyDict = keyDictUsed.key;
308
+ if (keyDict.changed === schemaBuffer.keyChangeIndex) {
309
+ const addressSize = schemaBuffer.buf[keyDictUsed.address];
310
+ // aslo correct if its smaller... :|
311
+ if (addressSize === KEY_ADDRESS_3_BYTES) {
312
+ writeUint24(schemaBuffer.buf, keyDict.address, keyDictUsed.address + 1);
313
+ }
314
+ else if (addressSize === KEY_ADDRESS_2_BYTES) {
315
+ writeUint16(schemaBuffer.buf, keyDict.address, keyDictUsed.address + 1);
316
+ }
317
+ else if (addressSize === KEY_ADDRESS_1_BYTE) {
318
+ schemaBuffer.buf[keyDictUsed.address + 1] = keyDict.address;
319
+ }
320
+ }
321
+ keyDictUsed.address -= 3;
322
+ }
323
+ }
324
+ schemaBuffer.buf.copyWithin(sizeIndex + 1, sizeIndex + 4, sizeIndex + size);
325
+ schemaBuffer.len -= 3;
326
+ }
327
+ else {
328
+ schemaBuffer.buf[sizeIndex] = 0; // means 4
329
+ writeUint24(schemaBuffer.buf, size, sizeIndex + 1);
330
+ }
286
331
  };
287
332
  export const serialize = (schema, opts = {}) => {
288
333
  if (!schemaBuffer) {
289
334
  schemaBuffer = {
290
- buf: new Uint8Array(10e3), // 10kb default
335
+ buf: new Uint8Array(5e3), // 5kb default
291
336
  len: 0,
292
337
  dictMap: {},
338
+ dictMapArr: [],
339
+ dictMapUsed: [],
340
+ keyChangeIndex: 0,
293
341
  };
294
342
  }
343
+ schemaBuffer.keyChangeIndex = 0;
295
344
  schemaBuffer.len = 0;
296
345
  schemaBuffer.dictMap = {};
346
+ schemaBuffer.dictMapArr = [];
347
+ schemaBuffer.dictMapUsed = [];
297
348
  // defalte not supported in unpacking yet
298
349
  const isDeflate = 0; // opts.deflate ? 1 : 0
299
350
  walk(opts, schema, undefined, undefined, false, schemaBuffer);
@@ -302,6 +353,7 @@ export const serialize = (schema, opts = {}) => {
302
353
  // // add extra byte! see if nessecary
303
354
  // return deflate.deflateSync(packed)
304
355
  // } else {
356
+ // console.log('USED', schemaBuffer.dictMapUsed.length)
305
357
  return packed;
306
358
  // }
307
359
  };
@@ -348,12 +400,11 @@ export const deSerializeInner = (buf, obj, start, fromArray) => {
348
400
  }
349
401
  let size;
350
402
  if (buf[i] === 0) {
351
- size = readUint32(buf, i + 1);
352
- i += 5;
403
+ size = readUint24(buf, i + 1);
404
+ i += 4;
353
405
  }
354
406
  else {
355
407
  size = buf[i] - 3;
356
- console.log('yo', size);
357
408
  i += 1;
358
409
  }
359
410
  const end = size + start;
package/dist/types.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { LangName } from './lang.js';
2
2
  import { Validation } from './def/validation.js';
3
+ import { Transform } from './def/types.js';
3
4
  type Role = 'title' | 'source' | 'media' | string;
4
5
  export declare const numberDisplays: readonly ["short", "human", "ratio", "bytes", "euro", "dollar", "pound", "meter"];
5
6
  export declare const dateDisplays: readonly ["date", "date-time", "date-time-text", "date-time-human", "time", "time-precise"];
6
- export declare const stringFormats: readonly ["alpha", "alphaLocales", "alphanumeric", "alphanumericLocales", "ascii", "base32", "base58", "base64", "BIC", "btcAddress", "clike", "code", "creditCard", "css", "currency", "dataURI", "EAN", "email", "ethereumAddress", "FQDN", "hexadecimal", "hexColor", "HSL", "html", "IBAN", "identityCard", "IMEI", "IP", "IPRange", "ISBN", "ISIN", "ISO31661Alpha2", "ISO31661Alpha3", "ISO4217", "ISO6391", "ISO8601", "ISRC", "ISSN", "javascript", "json", "JWT", "latLong", "licensePlate", "lowercase", "luhnNumber", "MACAddress", "magnetURI", "markdown", "MD5", "mimeType", "mobilePhone", "mobilePhoneLocales", "octal", "passportNumber", "port", "postalCode", "postalCodeLocales", "python", "RFC3339", "rgbColor", "rust", "semVer", "slug", "strongPassword", "surrogatePair", "taxID", "typescript", "uppercase", "URL", "UUID", "VAT", "multiline"];
7
+ export declare const stringFormats: readonly ["alpha", "alphaLocales", "alphanumeric", "alphanumericLocales", "ascii", "base32", "base58", "base64", "BIC", "btcAddress", "clike", "code", "creditCard", "css", "currency", "dataURI", "EAN", "email", "ethereumAddress", "FQDN", "hexadecimal", "hexColor", "HSL", "html", "IBAN", "identityCard", "IMEI", "IP", "IPRange", "ISBN", "ISIN", "ISO31661Alpha2", "ISO31661Alpha3", "ISO4217", "ISO6391", "ISO8601", "ISRC", "ISSN", "javascript", "json", "JWT", "latLong", "licensePlate", "lowercase", "luhnNumber", "MACAddress", "magnetURI", "markdown", "MD5", "mimeType", "mobilePhone", "mobilePhoneLocales", "octal", "password", "passportNumber", "port", "postalCode", "postalCodeLocales", "python", "RFC3339", "rgbColor", "rust", "semVer", "slug", "surrogatePair", "taxID", "typescript", "uppercase", "URL", "UUID", "VAT", "multiline"];
7
8
  type DateDisplay = (typeof dateDisplays)[number];
8
9
  type NumberDisplay = (typeof numberDisplays)[number] | `round-${number}`;
9
10
  type StringFormat = (typeof stringFormats)[number];
@@ -16,6 +17,7 @@ type PropValues = {
16
17
  type?: string;
17
18
  default?: any;
18
19
  validation?: Validation;
20
+ transform?: Transform;
19
21
  };
20
22
  type Prop<V extends PropValues> = {
21
23
  required?: boolean;
@@ -27,6 +29,7 @@ type Prop<V extends PropValues> = {
27
29
  readOnly?: boolean;
28
30
  examples?: string[];
29
31
  validation?: Validation;
32
+ transform?: Transform;
30
33
  } & V;
31
34
  type EnumItem = string | number | boolean;
32
35
  type NeverInItems = {
package/dist/types.js CHANGED
@@ -71,6 +71,7 @@ export const stringFormats = [
71
71
  'mobilePhone',
72
72
  'mobilePhoneLocales',
73
73
  'octal',
74
+ 'password',
74
75
  'passportNumber',
75
76
  'port',
76
77
  'postalCode',
@@ -81,7 +82,6 @@ export const stringFormats = [
81
82
  'rust',
82
83
  'semVer',
83
84
  'slug',
84
- 'strongPassword',
85
85
  'surrogatePair',
86
86
  'taxID',
87
87
  'typescript',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.23",
3
+ "version": "5.0.0-alpha.24",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",