@atproto/lex-builder 0.0.11 → 0.0.12

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.
@@ -434,10 +434,16 @@ export class LexDefBuilder {
434
434
  if (hash === 'main' && validationUtils) {
435
435
  this.addUtils({
436
436
  $assert: markPure(`${ref.varName}.assert.bind(${ref.varName})`),
437
+ $check: markPure(`${ref.varName}.check.bind(${ref.varName})`),
438
+ $cast: markPure(`${ref.varName}.cast.bind(${ref.varName})`),
437
439
  $ifMatches: markPure(`${ref.varName}.ifMatches.bind(${ref.varName})`),
438
440
  $matches: markPure(`${ref.varName}.matches.bind(${ref.varName})`),
439
441
  $parse: markPure(`${ref.varName}.parse.bind(${ref.varName})`),
440
442
  $safeParse: markPure(`${ref.varName}.safeParse.bind(${ref.varName})`),
443
+ $validate: markPure(`${ref.varName}.validate.bind(${ref.varName})`),
444
+ $safeValidate: markPure(
445
+ `${ref.varName}.safeValidate.bind(${ref.varName})`,
446
+ ),
441
447
  })
442
448
  }
443
449
 
@@ -474,7 +480,11 @@ export class LexDefBuilder {
474
480
  if (!def) return this.pure(`l.params()`)
475
481
 
476
482
  const properties = await this.compilePropertiesSchemas(def)
477
- return this.pure(`l.params({${properties.join(',')}})`)
483
+ return this.pure(
484
+ properties.length === 0
485
+ ? `l.params()`
486
+ : `l.params({${properties.join(',')}})`,
487
+ )
478
488
  }
479
489
 
480
490
  private async compileErrors(defs?: readonly LexiconError[]) {
@@ -645,13 +655,24 @@ export class LexDefBuilder {
645
655
  return `l.UnknownObject`
646
656
  }
647
657
 
658
+ private withDefault(schema: string, defaultValue: unknown) {
659
+ if (defaultValue === undefined) return schema
660
+
661
+ return this.pure(
662
+ `l.withDefault(${schema}, ${JSON.stringify(defaultValue)})`,
663
+ )
664
+ }
665
+
648
666
  private async compileBooleanSchema(def: LexiconBoolean): Promise<string> {
667
+ const schema = l.boolean()
668
+
669
+ if (def.default !== undefined) {
670
+ schema.check(def.default)
671
+ }
672
+
649
673
  if (hasConst(def)) return this.compileConstSchema(def)
650
674
 
651
- const options = stringifyOptions(def, [
652
- 'default',
653
- ] satisfies (keyof l.BooleanSchemaOptions)[])
654
- return this.pure(`l.boolean(${options})`)
675
+ return this.withDefault(this.pure(`l.boolean()`), def.default)
655
676
  }
656
677
 
657
678
  private async compileBooleanType(def: LexiconBoolean): Promise<string> {
@@ -660,25 +681,29 @@ export class LexDefBuilder {
660
681
  }
661
682
 
662
683
  private async compileIntegerSchema(def: LexiconInteger): Promise<string> {
684
+ const schema = l.integer(def)
685
+
663
686
  if (hasConst(def)) {
664
- const schema: l.IntegerSchema = l.integer(def)
665
- schema.assert(def.const)
687
+ schema.check(def.const)
666
688
  }
667
689
 
668
690
  if (hasEnum(def)) {
669
- const schema: l.IntegerSchema = l.integer(def)
670
- for (const val of def.enum) schema.assert(val)
691
+ for (const val of def.enum) schema.check(val)
692
+ }
693
+
694
+ if (def.default !== undefined) {
695
+ schema.check(def.default)
671
696
  }
672
697
 
673
698
  if (hasConst(def)) return this.compileConstSchema(def)
674
699
  if (hasEnum(def)) return this.compileEnumSchema(def)
675
700
 
676
701
  const options = stringifyOptions(def, [
677
- 'default',
678
702
  'maximum',
679
703
  'minimum',
680
704
  ] satisfies (keyof l.IntegerSchemaOptions)[])
681
- return this.pure(`l.integer(${options})`)
705
+
706
+ return this.withDefault(this.pure(`l.integer(${options})`), def.default)
682
707
  }
683
708
 
684
709
  private async compileIntegerType(def: LexiconInteger): Promise<string> {
@@ -689,26 +714,32 @@ export class LexDefBuilder {
689
714
  }
690
715
 
691
716
  private async compileStringSchema(def: LexiconString): Promise<string> {
717
+ const schema = l.string(def)
718
+
692
719
  if (hasConst(def)) {
693
- const schema: l.StringSchema = l.string(def)
694
- schema.assert(def.const)
695
- } else if (hasEnum(def)) {
696
- const schema: l.StringSchema = l.string(def)
697
- for (const val of def.enum) schema.assert(val)
720
+ schema.check(def.const)
721
+ }
722
+
723
+ if (hasEnum(def)) {
724
+ for (const val of def.enum) schema.check(val)
725
+ }
726
+
727
+ if (def.default !== undefined) {
728
+ schema.check(def.default)
698
729
  }
699
730
 
700
731
  if (hasConst(def)) return this.compileConstSchema(def)
701
732
  if (hasEnum(def)) return this.compileEnumSchema(def)
702
733
 
703
734
  const options = stringifyOptions(def, [
704
- 'default',
705
735
  'format',
706
736
  'maxGraphemes',
707
737
  'minGraphemes',
708
738
  'maxLength',
709
739
  'minLength',
710
740
  ] satisfies (keyof l.StringSchemaOptions)[])
711
- return this.pure(`l.string(${options})`)
741
+
742
+ return this.withDefault(this.pure(`l.string(${options})`), def.default)
712
743
  }
713
744
 
714
745
  private async compileStringType(def: LexiconString): Promise<string> {
@@ -783,7 +814,7 @@ export class LexDefBuilder {
783
814
  }
784
815
 
785
816
  private async compileCidLinkSchema(_def: LexiconCid): Promise<string> {
786
- return this.pure(`l.cidLink()`)
817
+ return this.pure(`l.cid()`)
787
818
  }
788
819
 
789
820
  private async compileCidLinkType(_def: LexiconCid): Promise<string> {
@@ -825,10 +856,10 @@ export class LexDefBuilder {
825
856
  const types = await Promise.all(
826
857
  def.refs.map(async (ref) => {
827
858
  const { typeName } = await this.refResolver.resolve(ref)
828
- return `l.TypedRef<${typeName}>`
859
+ return `l.$Typed<${typeName}>`
829
860
  }),
830
861
  )
831
- if (!def.closed) types.push('l.TypedObject')
862
+ if (!def.closed) types.push('l.Unknown$TypedObject')
832
863
  return types.join(' | ') || 'never'
833
864
  }
834
865
 
@@ -839,10 +870,9 @@ export class LexDefBuilder {
839
870
  return this.pure(`l.never()`)
840
871
  }
841
872
 
842
- const options = stringifyOptions(def, [
843
- 'default',
844
- ] satisfies (keyof l.LiteralSchemaOptions<any>)[])
845
- return this.pure(`l.literal(${JSON.stringify(def.const)}, ${options})`)
873
+ const result = this.pure(`l.literal(${JSON.stringify(def.const)})`)
874
+
875
+ return this.withDefault(result, def.default)
846
876
  }
847
877
 
848
878
  private async compileConstType<
@@ -861,13 +891,13 @@ export class LexDefBuilder {
861
891
  if (def.enum.length === 0) {
862
892
  return this.pure(`l.never()`)
863
893
  }
864
- if (def.enum.length === 1 && def.default === undefined) {
865
- return this.pure(`l.literal(${JSON.stringify(def.enum[0])})`)
866
- }
867
- const options = stringifyOptions(def, [
868
- 'default',
869
- ] satisfies (keyof l.EnumSchemaOptions<any>)[])
870
- return this.pure(`l.enum(${JSON.stringify(def.enum)}, ${options})`)
894
+
895
+ const result =
896
+ def.enum.length === 1
897
+ ? this.pure(`l.literal(${JSON.stringify(def.enum[0])})`)
898
+ : this.pure(`l.enum(${JSON.stringify(def.enum)})`)
899
+
900
+ return this.withDefault(result, def.default)
871
901
  }
872
902
 
873
903
  private async compileEnumType<T extends null | number | string>(def: {