@@ -109,12 +109,8 @@ export class Collection<Schema extends StandardSchemaV1> {
109109 let logger = this . #logger. extend ( 'create' )
110110 logger . log ( 'initial values:' , initialValues )
111111
112- const { sanitizedInitialValues, restoreProperties } =
113- this . #sanitizeInitialValues( initialValues )
114-
115- const validationResult = await this . options . schema [ '~standard' ] . validate (
116- sanitizedInitialValues ,
117- )
112+ const validationResult =
113+ await this . options . schema [ '~standard' ] . validate ( initialValues )
118114
119115 if ( validationResult . issues ) {
120116 console . error ( validationResult . issues )
@@ -134,8 +130,6 @@ export class Collection<Schema extends StandardSchemaV1> {
134130 initialValues ,
135131 )
136132
137- restoreProperties ( record )
138-
139133 // Generate random primary key for every record.
140134 const primaryKey =
141135 ( isObject ( initialValues ) &&
@@ -524,82 +518,6 @@ export class Collection<Schema extends StandardSchemaV1> {
524518 } )
525519 }
526520
527- /**
528- * Sanitizes the given object so it can be accepted as the input to Standard Schema validation.
529- * This removes getters to prevent potentially infinite object references in self-referencing
530- * relations. This also drops the internal symbols but gives a function to restore them back.
531- */
532- #sanitizeInitialValues( initialValues : unknown ) {
533- const propertiesToRestore : Array < {
534- path : Array < string | number | symbol >
535- descriptor : PropertyDescriptor
536- } > = [ ]
537-
538- const sanitize = (
539- value : unknown ,
540- path : Array < string | number | symbol > = [ ] ,
541- ) : unknown => {
542- if ( Array . isArray ( value ) ) {
543- return value . map ( ( value , index ) => sanitize ( value , path . concat ( index ) ) )
544- }
545-
546- if ( isObject ( value ) ) {
547- const relations = isRecord ( value ) ? value [ kRelationMap ] : undefined
548-
549- return Object . fromEntries (
550- Reflect . ownKeys ( value ) . map ( ( key ) => {
551- const childValue = value [ key as keyof typeof value ]
552- const childPath = path . concat ( key )
553-
554- if ( typeof key === 'symbol' ) {
555- /**
556- * @note Preserve primary keys on sanitized initial values.
557- * Otherwise, internal symbols are stripped off and record references are lost.
558- * This is curcial when handling relations for records that were created
559- * before the relation was defined.
560- */
561- if ( key === kPrimaryKey ) {
562- propertiesToRestore . push ( {
563- path : childPath ,
564- descriptor : Object . getOwnPropertyDescriptor ( value , key ) ! ,
565- } )
566- }
567- return [ key , childValue ]
568- }
569-
570- const relation = relations ?. get ( key )
571-
572- if ( relation && childValue != null ) {
573- propertiesToRestore . push ( {
574- path : childPath ,
575- descriptor : Object . getOwnPropertyDescriptor ( value , key ) ! ,
576- } )
577- return [ key , relation . getDefaultValue ( ) ]
578- }
579-
580- return [ key , sanitize ( childValue , childPath ) ]
581- } ) ,
582- )
583- }
584-
585- return value
586- }
587-
588- const sanitizedInitialValues = sanitize ( initialValues )
589- return {
590- sanitizedInitialValues,
591- /**
592- * Restores record properties that were stripped off during the sanitization
593- * (e.g. relational properties, internal symbols of records given as initial value, etc).
594- */
595- restoreProperties ( record : RecordType ) : void {
596- for ( const { path, descriptor } of propertiesToRestore ) {
597- definePropertyAtPath ( record , path , descriptor )
598- }
599- } ,
600- }
601- }
602-
603521 * #query(
604522 query : Query < StandardSchemaV1 . InferOutput < Schema > > ,
605523 options : PaginationOptions < Schema > = { take : Infinity } ,
@@ -815,10 +733,8 @@ export class Collection<Schema extends StandardSchemaV1> {
815733 : maybeNextRecord
816734
817735 logger . log ( 're-applying the schema...' )
818- const { sanitizedInitialValues } = this . #sanitizeInitialValues( nextRecord )
819- const validationResult = await this . options . schema [ '~standard' ] . validate (
820- sanitizedInitialValues ,
821- )
736+ const validationResult =
737+ await this . options . schema [ '~standard' ] . validate ( nextRecord )
822738
823739 if ( validationResult . issues ) {
824740 console . error ( validationResult . issues )
0 commit comments